新的关键字如何在PHP中的对象变量旁边工作?

时间:2018-01-15 17:43:21

标签: php class keyword

在PHP中学习OOP时,我注意到@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); GoogleSignInApi.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()){ GoogleSignInAccount account = result.getSignInAccount(); firebaseAuthWithGoogle(account); } else { // Google Sign In failed, update UI appropriately Toast.makeText(GoogleLoginActivity.this,"Something went wrong 2",Toast.LENGTH_SHORT).show(); // ... } } } 中的Here表示“' new'在对象变量旁边使用的关键字创建了一个新对象 我在这里举一个例子:

Example #5 Creating new objects

另一个例子:

    Class Test {
          function getInstance(){
           return new $this;
          }
   }

$Object1 = new Test();
$Object2 = $Object1->getInstance(); // this will create new object from the same class

The documentation 没有提及任何声明,我也没有找到任何进一步的参考资料来澄清这个问题。

这是如何工作的,尽管使用' new'关键字说明了下面提到的规则:

  

如果包含类名称的字符串与new一起使用,则将创建该类的新实例。如果类在命名空间中,则在执行此操作时必须使用其完全限定名称。

1 个答案:

答案 0 :(得分:-1)

PHP Documentation中引用了“示例#5”,其中描述了PHP 5.3.0 introduced a couple of new ways to create instances of an object

我对this related question的回答更详细地解释了new class()new $class()clone $class之间的差异