new self(); 是什么意思? PHP 中的意思?
我从未见过这样的代码:
public static function getInstance()
{
if ( ! isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
它与 new className() 相同吗?
编辑
如果类是继承的,它指向哪个类?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
self
指向编写它的类。因此,如果您的 getInstance 方法位于类名
MyClass
中,则以下行:将执行与以下相同的操作:
编辑:在注释后提供更多信息。
如果您有两个相互扩展的类,有两种情况:
getInstance
在子类中定义getInstance
在父类中定义第一种情况如下所示(我对于本例,已删除所有不必要的代码 - 您必须将其添加回来才能获得单例行为)*:
在这里,您将得到:
这意味着
self
意味着MyChildClass
——即编写它的类。For the second situation, the code would look like this :
你会得到这样的输出:
这意味着
self
意味着MyParentClass
——即这里,编写它的类。With PHP That's why PHP 5.3 introduces a new usage for the
static
keyword : it can now be used exactly where we usedself
in those examples :但是,使用
static
而不是self
,您现在将得到:这意味着
static
有点指向 使用(我们使用MyChildClass::getInstance()
),而不是编写它的那个。当然,为了不破坏现有应用程序,
self
的行为没有改变——PHP 5.3 只是添加了一个新行为,回收了static
关键字。And, speaking about PHP 5.3, you might want to take a look at the [Late Static Bindings][1] page of the PHP manual.
self
points to the class in which it is written.So, if your getInstance method is in a class name
MyClass
, the following line :Will do the same as :
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 classgetInstance
is defined in the parent classThe 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)* :
Here, you'll get :
Which means
self
meansMyChildClass
-- i.e. the class in which it is written.For the second situation, the code would look like this :
And you'd get this kind of output :
Which means
self
meansMyParentClass
-- 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 usedself
in those examples :But, with
static
instead ofself
, you'll now get :Which means that
static
sort of points to the class that is used (we usedMyChildClass::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 thestatic
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.
这似乎是单例模式的实现。
该函数被静态调用并检查静态类是否设置了变量
$_instance
。如果不是,它会初始化自身的一个实例 (
new self()
) 并将其存储在$_instance
中。如果您调用
className::getInstance()
,您将在每次调用时获得一个且相同的类实例,这就是单例模式的要点。不过,我从来没有见过这样的做法,而且说实话,我也不知道这是可能的。
类中的
$_instance
声明是什么?This seems to be an implementation of the Singleton pattern.
The function is called statically and checks whether the static class has the variable
$_instance
set.If it isn't, it initializes an instance of itself (
new self()
) and stores it in$_instance
.If you call
className::getInstance()
you will get one and the same class instance on every call, which is the point of the singleton pattern.I've never seen it this done this way, though, and honestly didn't know it was possible.
What is
$_instance
declared as in the class?这最有可能在单例设计模式中使用,其中构造函数被定义为私有以避免被实例化,双冒号
(::)
运算符可以访问类内声明为静态的成员,因此,如果有静态成员,则不能使用伪变量 $this,因此代码使用 self 代替,单例是良好的编程实践,它只允许对象的 1 个实例,如数据库连接器处理程序。从客户端代码中,访问该实例将通过创建单个访问点来完成,在本例中他将其命名为getInstance()
, getInstance 本身就是基本上使用 new 创建对象的函数创建对象的关键字意味着还调用了构造函数方法。if(!isset(self::instance)) 行检查是否已经创建了一个对象,您无法理解这一点,因为代码只是一个片段,在顶部的某个地方,应该有静态成员就像
在普通类中一样,我们可以简单地通过
其声明为静态来访问此成员,因此我们不能使用 $this 代码,而是
通过检查此静态类变量上是否存储了对象,然后该类可以决定创建或不创建单个实例,因此如果未设置!isset,意味着静态成员$_instance上不存在对象,则它会生成一个新对象,并将其存储在静态成员
$_instance< /code> 通过命令
并将其返回给客户端代码。然后,客户端代码可以愉快地使用该对象的单个实例及其公共方法,但是在客户端代码中,调用单个访问点,即
getInstance()
方法也很棘手,它必须这样调用,原因是函数本身被声明为静态。
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 itgetInstance()
, 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.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 probablyin normal classes we would have accessed this member by simply
but its declared static and so we could not use the $this code we use instead
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
$_instance
by the commandand 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 thisthe reason, the function in itself is declared static.
是的,它就像
new className()
(指的是包含该方法的类),可能在构造函数是私有的单例模式中使用。Yes, it's like
new className()
(referring to the class containing that method), probably used in a Singleton pattern where the constructor is private.如果该类是继承的,那么从 child 调用 getInstance() 不会为您提供 child 的实例。它只会返回父实例的实例。
这是因为我们调用了 new self()。
如果您希望子类返回子类的实例,则在 getInstance() 中使用 new static() ,然后它将返回子类实例。这就是所谓的后期绑定!
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().
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!!