PHP $this variable

前端 未结 6 1262
日久生厌
日久生厌 2020-12-29 22:28

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         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-29 22:47

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

提交回复
热议问题