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
?
If the method of this code is not static, you can get a work-around in 5.2 by using
get_class($this)
.The results:
Not really. I don't know of a workaround for PHP 5.2, though.
self
refers to the same class in which thenew
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 fromA
. Theself
invocation is bound toA
because it's defined inA
's implementation of the first method, whereasstatic
is bound to the called class (also seeget_called_class()
).In addition to others' answers :
That means you can't use
static::
in a class property because properties values :Using
self::