PHP で遅延静的バインディングをコーディングして使用しているときに、奇妙な動作を見つけました。static()
親クラスで作成された子オブジェクトは、親のプライベート メソッドにアクセスできます。
次に例を示します。
class Attachment
{
public static function createFromFile($file)
{
$attachment = new static();
echo get_class($attachment) . PHP_EOL;
$attachment->loadFromFile($file);
}
private function loadFromFile($file)
{
echo 'attachment';
}
}
class PictureAttachment extends Attachment
{
//...
}
PictureAttachment::createFromFile('example.txt');
出力:
PictureAttachment
attachment
これは正しい動作ですか?