Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New self vs. new static

I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options); , if I convert this to return new self($options) will I get the same results?

What is the difference between new self and new static?

like image 808
Mike Avatar asked Mar 04 '11 17:03

Mike


People also ask

What is the difference between static and self?

self Vs static: The most basic difference between them is that self points to the version of the property of the class in which it is declared but in case of static, the property undergoes a redeclaration at runtime.

What is the difference between using self and this?

The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.

What does self :: mean in PHP?

self operator: self operator represents the current class and thus is used to access class variables or static variables because these members belongs to a class rather than the object of that class.

What is return static?

static return type follows Liskov Substitution Principle. A child class method can return a narrower class object than the parent methods return type. Because static always refer to the class name of the called object (i.e. same as get_class($object) ), static is a subset of self , which in turn is subset of parent .


2 Answers

will I get the same results?

Not really. I don't know of a workaround for PHP 5.2, though.

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()).

class A {     public static function get_self() {         return new self();     }      public static function get_static() {         return new static();     } }  class B extends A {}  echo get_class(B::get_self());  // A echo get_class(B::get_static()); // B echo get_class(A::get_self()); // A echo get_class(A::get_static()); // A 
like image 155
BoltClock Avatar answered Sep 23 '22 02:09

BoltClock


If the method of this code is not static, you can get a work-around in 5.2 by using get_class($this).

class A {     public function create1() {         $class = get_class($this);         return new $class();     }     public function create2() {         return new static();     } }  class B extends A {  }  $b = new B(); var_dump(get_class($b->create1()), get_class($b->create2())); 

The results:

string(1) "B" string(1) "B" 
like image 28
Marius Balčytis Avatar answered Sep 24 '22 02:09

Marius Balčytis