While coding and using late static binding in PHP I found some strange behaviour. A child object created with static()
in its parent class can access the private methods of its parent.
Here's an example:
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');
Output:
PictureAttachment
attachment
Is this a correct behaviour?