为什么这个 php 代码的最后一个语句中有两个$this


Why are there two $this in the last statement in this php code?

 parent::__construct();
    $this->displayName = $this->l('Responsive products featured');
    $this->description = $this->l('Displays featured products and categories in your homepage.');
    $this->
    include_once($this->local_path.'/classes/ResponsiveHomeFeaturedClass.php');

你会发现很难理解为什么最后一条语句使用两个$this?

(我不确定粘贴的代码中是否有错误,因为include_once是一个常用的内置函数,但我想它也可能是对象的实例函数......

任何

一行代码都没有理由不能引用$this两次,或者根据需要多次引用。 $this只是对对象的引用。 所以在这一行代码中:

$this->include_once($this->local_path.'/classes/ResponsiveHomeFeaturedClass.php');

它只是调用 include_once 函数并向其传递一个值,其中包括local_path值。 这将完成同样的事情:

$some_local_path = $this->local_path;
$this->include_once($some_local_path.'/classes/ResponsiveHomeFeaturedClass.php');

但它将使用不必要的临时变量。 include_once只是一个函数,local_path只是一个值。 后者可以用作前者的参数。

你不能用include_once作为你的方法,因为它是保留的关键字!

以下代码

class Test {
  function include_once() {
  }
}

将不起作用:

Parse error: syntax error, unexpected T_INCLUDE_ONCE, expecting T_STRING

让你远离麻烦是一件非常好的事情。

如果include_once确实是一种方法(它不可能),则有问题的代码将起作用,但是,插入空行不是一个好的做法。

关于$this - 它是方法或属性调用的一部分,因此通常可以根据需要多次使用它来引用您的方法或属性(来自同一类实例)。