new self() 是做什么的;在 PHP 中是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2396415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does new self(); mean in PHP?
提问by user198729
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()?
是一样的new className()吗?
EDIT
编辑
If the class is inheritant,which class does it point to?
如果类是继承的,它指向哪个类?
回答by Pascal MARTIN
selfpoints to the class in which it is written.
self指向编写它的类。
So, if your getInstance method is in a class name MyClass, the following line :
因此,如果您的 getInstance 方法在类名中MyClass,则以下行:
self::$_instance = new self();
Will do the same as :
将做同样的事情:
self::$_instance = new MyClass();
Edit : a couple more informations, after the comments.
编辑:在评论后提供更多信息。
If you have two classes that extend each other, you have two situations :
如果你有两个相互扩展的类,你有两种情况:
getInstanceis defined in the child classgetInstanceis defined in the parent class
getInstance在子类中定义getInstance在父类中定义
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 selfmeans MyChildClass-- i.e. the class in which it is written.
这意味着self意味着MyChildClass- 即它被写入的类。
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 selfmeans MyParentClass-- i.e. here too, the class in which it is written.
这意味着self意味着MyParentClass- 即这里也是,它被写入的类。
With PHP < 5.3, that "the class in which it is written" is important -- and can sometimes cause problems.
对于 PHP < 5.3,“编写它的类”很重要——有时会导致问题。
That's why PHP 5.3 introduces a new usage for the statickeyword : it can now be used exactly where we used selfin those examples :
这就是 PHP 5.3 为static关键字引入新用法的原因:现在可以在我们self在这些示例中使用的地方使用它:
class MyParentClass {
public static function getInstance() {
return new static();
}
}
class MyChildClass extends MyParentClass {
}
$a = MyChildClass::getInstance();
var_dump($a);
But, with staticinstead of self, you'll now get :
但是,使用static代替self,您现在将获得:
object(MyChildClass)#1 (0) { }
Which means that staticsort of points to the class that is used(we used MyChildClass::getInstance()), and not the one in which it is written.
这意味着static那种指向使用的类(我们使用过MyChildClass::getInstance()),而不是编写它的类。
Of course, the behavior of selfhas not been changed, to not break existing applications -- PHP 5.3 just added a new behavior, recycling the statickeyword.
当然,行为self并没有改变,为了不破坏现有的应用程序——PHP 5.3 只是增加了一个新的行为,回收static关键字。
And, speaking about PHP 5.3, you might want to take a look at the Late Static Bindingspage of the PHP manual.
而且,谈到 PHP 5.3,您可能需要查看PHP 手册的Late Static Bindings页面。
回答by Pekka
This seems to be an implementation of the Singleton pattern.
The function is called statically and checks whether the static class has the variable $_instanceset.
这似乎是Singleton 模式的实现。该函数被静态调用并检查静态类是否具有变量$_instance集。
If it isn't, it initializes an instance of itself (new self()) and stores it in $_instance.
如果不是,它会初始化自己的一个实例 ( new self()) 并将其存储在$_instance.
If you call className::getInstance()you will get one and the sameclass instance on every call, which is the point of the singleton pattern.
如果您调用,className::getInstance()您将在每次调用时获得一个相同的类实例,这就是单例模式的重点。
I've never seen it this done this way, though, and honestly didn't know it was possible.
What is $_instancedeclared as in the class?
不过,我从未见过以这种方式这样做,老实说,我不知道这是可能的。什么是$_instance声明为类?
回答by flimh
This is most likely used in singleton design pattern, wherein the constructor is defined as private so as to avoid being instantiated, the double colon (::)operator can access members that are declared static inside the class, so if there are static members, the pseudo variable $this cannot be used, hence the code used self instead, Singletons are good programming practices that will only allow 1 instance of an object like database connector handlers. From client code, accessing that instance would be done by creating a single access point, in this case he named it getInstance(), The getInstance in itself was the function that created the the object basically using the new keyword to create an object meaning the constructor method was also called.
这很可能用于单例设计模式,其中构造函数定义为私有以避免被实例化,双冒号(::)运算符可以访问类内部声明为静态的成员,因此如果有静态成员,伪变量 $这不能使用,因此代码使用 self 代替,单例是良好的编程实践,它只允许 1 个对象实例,如数据库连接器处理程序。从客户端代码中,访问该实例将通过创建单个访问点来完成,在这种情况下,他将其命名getInstance()为也被称为。
the line if(!isset(self::instance))checks if an object has already been created, you could not understand this becuase the code is just a fragment, somewhere in the top, there should be static members like probably
该行if(!isset(self::instance))检查是否已经创建了一个对象,您无法理解这一点,因为代码只是一个片段,在顶部某处,应该有静态成员,例如可能
private static $_instance = NULL;
in normal classes we would have accessed this member by simply
在普通的类中,我们可以通过简单地访问这个成员
$this->_instance = 'something';
but its declared static and so we could not use the $this code we use instead
但它声明为静态,所以我们不能使用我们使用的 $this 代码
self::$_instance
by checking if there is an object stored on this static class variable, the class can then decide to create or not to create a single instance, so if its not set, !isset, meaning no object exists on the static member $_instance, then it generates a new object, stored it in the static member $_instanceby the command
通过检查是否有一个对象存储在这个静态类变量上,该类可以决定创建或不创建单个实例,所以如果它没有设置,!isset,意味着静态成员 $_instance 上不存在对象,然后它生成一个新对象,$_instance通过命令将其存储在静态成员中
self::$_instance = new self();
and returned it to client code. The client code can then happily use the single instance of the object with its public methods, but in the client code, calling the single access point, that is, the getInstance()method is also tricky, it has to be called like this
并将其返回给客户端代码。客户端代码然后可以愉快地使用对象的单个实例及其公共方法,但是在客户端代码中,调用单个访问点,即getInstance()方法也很棘手,必须像这样调用
$thisObject = className::getInstance();
the reason, the function in itself is declared static.
原因是函数本身被声明为静态的。
回答by Matteo Riva
Yes, it's like new className()(referring to the class containing that method), probably used in a Singleton pattern where the constructor is private.
是的,它就像new className()(指包含该方法的类),可能在构造函数是私有的单例模式中使用。
回答by kta
If the class is inherited then calling getInstance() from child will not give you a instance of child. It will only returns an instance of parent instance. This is because we call new self().
如果该类是继承的,则从 child 调用 getInstance() 不会为您提供 child 的实例。它只会返回父实例的一个实例。这是因为我们调用了 new self()。
If you want that the child class will return an instance of child class then use new static() in the getInstance() and it will then return the child class instance. This is called late binding!!
如果您希望子类将返回子类的实例,则在 getInstance() 中使用 new static() ,然后它将返回子类实例。这叫做后期绑定!!

