If you are a lazy programmer, you may be annoyed when you see the following code
abstract class U{
}
class u1 extends U{
public static function create(){
return new u1();
}
}
class u2 extends U{
public static function create(){
return new u2();
}
}
There is no problem for this code to work normally, but a large amount of repeated code will be very annoying
I don’t want to add a create method in each subclass. If I put the create method in the super class U, the code may be
abstract class U{
public static function create(){
return new self();
}
}
class u1 extends U{
Function a(){}
}
class u2 extends U{
}
u1::create();
It looks very elegant and neat, now we put the common code in one place and use self as a reference to the class. But here we make an assumption about self.
In fact, the role of self on the class is not exactly the same as the role of $this on the object. self does not refer to the calling context, it refers to the parsing context, so if you run the above example, you will get
Fatal error: Cannot instantiate abstract class U in D:wampwwwtestoopstatic.php on line 21
Therefore self is resolved to the U that defines create, rather than to the u1 class that calls self.
Before php5.3, there were strict restrictions in this regard, resulting in many clumsy solutions. php5.3 introduced delayed static binding and the use of keyword static
Static is similar to self, but it refers to the called class rather than the containing class.
In the following example u1::create will generate the u1 object instead of instantiating the U object
abstract class U{
public static function create(){
Return new static();
}
}
class u1 extends U{
}
class u2 extends U{
}
u1::create();
Static can not only be used for instantiation, but like self and parent, it can also be used as a calling identifier for static methods, even calling from a non-static context
abstract class U{
private $group;
public function __construct(){
$this->group=static::getGroup();
}
public static function create(){
Return new static();
}
static function getGroup(){
return 'default';
}
}
class u1 extends U{
}
class u2 extends U{
static function getGroup(){
return 'u2';
}
}
class u3 extends u2{
}
print_r(u1::create());
echo '
';
print_r(u3::create());
u1 Object ( [group:U:private] => default )
u3 Object ( [group:U:private] => u2 )