PHP $this variable

2020-02-24 12:22发布

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

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:

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

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

  • Does $this belong class foo's data member? Why is there no declaration for $dbh in this class?

6条回答
趁早两清
2楼-- · 2020-02-24 12:31
  1. With the code you've posted, you can't know what the values of $this->dbh is.
  2. $dbh is a property of the current object. $this is 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->dbh can be used in any function inside the class.
  4. PHP doesn't require to define every variable you use.
查看更多
beautiful°
3楼-- · 2020-02-24 12:34
class foo {
  function select($p1, $dbh=null) {
    if ( is_null($dbh) )
        $this->dbh = $dbh ; 
    return; 
  }

  function get() {
    return $this->dbh; 
  }
}
查看更多
成全新的幸福
4楼-- · 2020-02-24 12:37

PHP is not strict about requiring class property declarations.

  • Upon assignation, the property is silently created.
  • Reading from a non-existent property generates a Notice if E_STRICT is enabled.
  • The default value for any undefined property is NULL
查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-24 12:38

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

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

}
查看更多
来,给爷笑一个
6楼-- · 2020-02-24 12:43

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.

Another thing to check is that you don't have any inheritance happening. Was your $dbh property 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. :-)

查看更多
7楼-- · 2020-02-24 12:49

What is the value of $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?

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

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

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.

查看更多
登录 后发表回答