Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

class - What does new self(); mean in PHP?

I've never seen code like this:

public static function getInstance()
{
    if ( ! isset(self::$_instance)) {
        self::$_instance = new self();
    }
    return self::$_instance;
}

Is it the same as new className() ?

EDIT

If the class is inheritant,which class does it point to?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

self points to the class in which it is written.

So, if your getInstance method is in a class name MyClass, the following line :

self::$_instance = new self();

Will do the same as :

self::$_instance = new MyClass();


Edit : a bit more information, after the comments.

If you have two classes that extend each other, you have two situations :

  • getInstance is defined in the child class
  • getInstance is defined in the parent class

The first situation would look like this (I've removed all non-necessary code, for this example -- you'll have to add it back to get the singleton behavior)* :

class MyParentClass {
    
}
class MyChildClass extends MyParentClass {
    public static function getInstance() {
        return new self();
    }
}

$a = MyChildClass::getInstance();
var_dump($a);

Here, you'll get :

object(MyChildClass)#1 (0) { } 

Which means self means MyChildClass -- i.e. the class in which it is written.


For the second situation, the code would look like this :
class MyParentClass {
    public static function getInstance() {
        return new self();
    }
}
class MyChildClass extends MyParentClass {
    
}

$a = MyChildClass::getInstance();
var_dump($a);

And you'd get this kind of output :

object(MyParentClass)#1 (0) { }

Which means self means MyParentClass -- i.e. here too, the class in which it is written.




With PHP That's why PHP 5.3 introduces a new usage for the static keyword : it can now be used exactly where we used self in those examples :
class MyParentClass {
    public static function getInstance() {
        return new static();
    }
}
class MyChildClass extends MyParentClass {
    
}

$a = MyChildClass::getInstance();
var_dump($a);

But, with static instead of self, you'll now get :

object(MyChildClass)#1 (0) { } 

Which means that static sort of points to the class that is used (we used MyChildClass::getInstance()), and not the one in which it is written.

Of course, the behavior of self has not been changed, to not break existing applications -- PHP 5.3 just added a new behavior, recycling the static keyword.


And, speaking about PHP 5.3, you might want to take a look at the [Late Static Bindings][1] page of the PHP manual.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...