What is the difference between new self vs new static in PHP?
What is the difference between new self and new static? New self vs new static in PHP?
Self: Access to the class that declares it. Static: Access to the current object.
1. Example:
2. What is the difference between new self and new static?
- self refers to the same class in which the new keyword is actually written.
- static, in PHP 5.3’s late static bindings,
refers to whatever class in the hierarchy you called the method on. - In the following example, B inherits both methods from A.
The self invocation is bound to A because it’s defined in A’s implementation
of the first method, whereas static is bound to the called class (also see get_called_class()). - => Self: Access to the class that declares it. Static: Access to the current object.