new self(); 是什么意思? PHP 中的意思?

发布于 2024-08-24 03:50:41 字数 282 浏览 3 评论 0原文

我从未见过这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南七夏 2024-08-31 03:50:41

self 指向编写它的类。

因此,如果您的 getInstance 方法位于类名 MyClass 中,则以下行:

self::$_instance = new self();

将执行与以下相同的操作:

self::$_instance = new MyClass();


编辑:在注释后提供更多信息。

如果您有两个相互扩展的类,有两种情况:

  • getInstance 在子类中定义
  • getInstance 在父类中定义

第一种情况如下所示(我对于本例,已删除所有不必要的代码 - 您必须将其添加回来才能获得单例行为)*:

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

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

在这里,您将得到:

object(MyChildClass)#1 (0) { } 

这意味着 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);

你会得到这样的输出:

object(MyParentClass)#1 (0) { }

这意味着 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 used self in those examples :

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

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

但是,使用 static 而不是 self,您现在将得到:

object(MyChildClass)#1 (0) { } 

这意味着 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 :

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.

北城半夏 2024-08-31 03:50:41

这似乎是单例模式的实现。
该函数被静态调用并检查静态类是否设置了变量$_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?

定格我的天空 2024-08-31 03:50:41

这最有可能在单例设计模式中使用,其中构造函数被定义为私有以避免被实例化,双冒号 (::) 运算符可以访问类内声明为静态的成员,因此,如果有静态成员,则不能使用伪变量 $this,因此代码使用 self 代替,单例是良好的编程实践,它只允许对象的 1 个实例,如数据库连接器处理程序。从客户端代码中,访问该实例将通过创建单个访问点来完成,在本例中他将其命名为 getInstance(), getInstance 本身就是基本上使用 new 创建对象的函数创建对象的关键字意味着还调用了构造函数方法。

if(!isset(self::instance)) 行检查是否已经创建了一个对象,您无法理解这一点,因为代码只是一个片段,在顶部的某个地方,应该有静态成员就像

private static $_instance = NULL; 

在普通类中一样,我们可以简单地通过

$this->_instance = 'something';

其声明为静态来访问此成员,因此我们不能使用 $this 代码,而是

self::$_instance

通过检查此静态类变量上是否存储了对象,然后该类可以决定创建或不创建单个实例,因此如果未设置!isset,意味着静态成员$_instance上不存在对象,则它会生成一个新对象,并将其存储在静态成员$_instance< /code> 通过命令

self::$_instance = new self();

并将其返回给客户端代码。然后,客户端代码可以愉快地使用该对象的单个实例及其公共方法,但是在客户端代码中,调用单个访问点,即 getInstance() 方法也很棘手,它必须这样调用

$thisObject = className::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 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.

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

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

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 $_instance by the command

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

$thisObject = className::getInstance();

the reason, the function in itself is declared static.

ら栖息 2024-08-31 03:50:41

是的,它就像 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.

打小就很酷 2024-08-31 03:50:41

如果该类是继承的,那么从 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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文