PHP $this 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5494436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:37:35  来源:igfitidea点击:

PHP $this variable

phpvariableslocalglobal

提问by wordpressquestion

I am reading some PHP code that I could not understand:

我正在阅读一些我无法理解的 PHP 代码:

class foo {
  function select($p1, $dbh=null) {
    if ( is_null($dbh) )
        $dbh = $this->dbh ; 
    return; 
  }

  function get() {
    return $this->dbh; 
  }
}

I can't find $this->dbh ($dbh)declaration from the class. My questions are:

$this->dbh ($dbh)在课堂上找不到声明。我的问题是:

  • What is the value of $this->dbh?

  • Is it a local variable for function select()?

  • Does $thisbelong class foo's data member? Why is there no declaration for $dbhin this class?

  • 的价值是$this->dbh什么?

  • 它是函数的局部变量select()吗?

  • 是不是$this属于class foo的数据成员?为什么$dbh在这个类中没有声明?

采纳答案by Unsigned

PHP is not strict about requiring class property declarations.

PHP 对类属性声明的要求并不严格。

  • Upon assignation, the property is silently created.
  • Reading froma non-existent property generates a Notice if E_STRICTis enabled.
  • The default value for any undefined property is NULL
  • 一旦分配,默默地创建了财产。
  • 如果E_STRICT启用,则从不存在的属性读取会生成通知。
  • 任何未定义属性的默认值是 NULL

回答by wordpressquestion

PHP is not strict for declaration. $this->dbh is a class member. I did the following code to understand the concept:

PHP 对声明并不严格。$this->dbh 是一个类成员。我做了以下代码来理解这个概念:

class foo {

 function foo(){
     $this->dbh = "initial value"; 
 }

 function select($p1, $dbh=null) {
    if ( is_null($dbh) )
        $dbh = $this->dbh ; 
    return; 
 }

 function get() {
     return $this->dbh; 
 }

}

It is same as:

它与:

class foo {
  var $dbh = "initial value"; 

  function select($p1, $dbh=null) {
    if ( is_null($dbh) )
       $dbh = $this->dbh ; 
    return; 
  }

  function get() {
     return $this->dbh; 
  }

}

回答by EmCo

  1. With the code you've posted, you can't know what the values of $this->dbhis.
  2. $dbhis a property of the current object. $thisis use to access to the members of the current object.
  3. Since this variable is defined outside of any function, is a variable that belongs to the class and not to a specific function. Because of this, $this->dbhcan be used in any function inside the class.
  4. PHP doesn't require to define every variable you use.
  1. 使用您发布的代码,您无法知道 的值$this->dbh是什么。
  2. $dbh是当前对象的属性。$this用于访问当前对象的成员。
  3. 由于此变量是在任何函数之外定义的,因此是属于类而不是特定函数的变量。正因为如此,$this->dbh可以在类内部的任何函数中使用。
  4. PHP 不需要定义您使用的每个变量。

回答by Chetan Sharma

What is the value of $this->dbh

$this->dbh 的值是多少

It will have the default value, if assigned else "null"

它将具有默认值,如果分配其他“空”

Is it a local variable for function select()? If it is, then why get() function can use this variable?

它是函数 select() 的局部变量吗?如果是,那么为什么 get() 函数可以使用这个变量?

It is the property of foo class, not the local variable, so it will be available to all the methods of the foo class

它是 foo 类的属性,而不是局部变量,因此它将可用于 foo 类的所有方法

Does it belongs class foo's data member? If it is, why there is no declaration for $dbh in this class?

它是否属于 foo 类的数据成员?如果是,为什么这个类中没有 $dbh 的声明?

Yes it does belong to the foo's data member, you don't see any declaration because, PHP is not strict about requiring class property declarations.

是的,它确实属于 foo 的数据成员,您看不到任何声明,因为 PHP 对要求类属性声明并不严格。

回答by Icode4food

PHP doesn't force you to declare you class properties but will create them for you when first accessed. Whether this is good or bad, be that as it may, welcome to PHP.

PHP 不会强制您声明类属性,而是会在第一次访问时为您创建它们。不管这是好是坏,不管怎样,欢迎来到 PHP。

Another thing to check is that you don't have any inheritance happening. Was your $dbhproperty defined in a parent class? There isn't anything in the simple code you posted but I can imagine that you simplified a bit for public consumption. :-)

要检查的另一件事是您没有发生任何继承。您的$dbh属性是否在父类中定义?您发布的简单代码中没有任何内容,但我可以想象您为公共消费做了一些简化。:-)

回答by Pritesh Loke

class foo {
  function select($p1, $dbh=null) {
    if ( is_null($dbh) )
        $this->dbh = $dbh ; 
    return; 
  }

  function get() {
    return $this->dbh; 
  }
}