什么是新的self();是什么意思? [英] What does new self(); mean in PHP?

查看:356
本文介绍了什么是新的self();是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有见过这样的代码:

I've never seen code like this:

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

是否与相同new className / code>?

Is it the same as new className() ?

EDIT

如果类是inheritant ,它指向哪个类?

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

推荐答案

self

因此,如果你的getInstance方法在类名 MyClass 中,行:

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

self::$_instance = new self();

会做同样的:

self::$_instance = new MyClass();


编辑:在评论后多了几条信息。

如果您有两个类彼此延伸,你有两种情况:

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


  • getInstance li>
  • getInstance 在父类中定义

  • 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);

在这里,您将获得:

object(MyChildClass)#1 (0) { } 

这意味着 self 意味着 MyChildClass - 即写入它的类。

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);

你会得到这样的输出:

object(MyParentClass)#1 (0) { }

这意味着 self 表示 MyParentClass - 也在这里,

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


br>
使用PHP< 5.3,写它的类很重要 - 有时可能会导致问题。


With PHP < 5.3, that "the class in which it is written" is important -- and can sometimes cause problems.

这就是为什么PHP 5.3引入了一个新的用法 static 关键字:现在可以准确地使用我们在这些示例中使用 self 的地方:

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 c $ c> self ,你现在可以得到:

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

object(MyChildClass)#1 (0) { } 

这意味着 static 对我们使用的类(我们使用 MyChildClass :: getInstance())的点类型,而不是写入它的类。

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.

当然, self 的行为没有改变,不破坏现有的应用程序添加了新的行为,回收了 static 关键字。

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.



5.3,您可能需要查看晚期静态绑定页面。

这篇关于什么是新的self();是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆