What is the difference between new self and new static in PHP? Late Static Binding
Hello everyone! I've been struggling with a strange behavior in PHP for three days. I have a base class that creates its instances through a static method. When I inherit this class, I expect the child class to create objects of its own type, but for some reason an object of the parent class is created.
Here's a simplified example:
```php
class ParentClass {
public static function create() {
return new self();
}
}
class ChildClass extends ParentClass {
// no override
}
$obj = ChildClass::create();
// $obj is ParentClass, not ChildClass!
echo get_class($obj); // ParentClass
```
I've read about Late Static Binding and the `static` keyword, but I don't fully understand how it works. Why does `new self()` always refer
No answers yet.
Log in to leave an answer