PHP $this variable

66,396

Solution 1

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

Solution 2

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

}

Solution 3

  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.

Solution 4

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. :-)

Solution 5

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.

Share:
66,396

Related videos on Youtube

wordpressquestion
Author by

wordpressquestion

Updated on July 09, 2022

Comments

  • wordpressquestion
    wordpressquestion almost 2 years

    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?

    • rockerest
      rockerest about 13 years
      Unless you haven't included code that defines $dbh for the class, or a class it extends, this code does nothing. What @Unsigned said is true: if a property is referenced that doesn't exist, it is silently created. In that case, however, this code ALWAYS returns null. ALWAYS. $this->dbh is never set, only used to set other variables, which means no matter what you do, get() will ALWAYS return null. I think perhaps you've left out some pertinent code, or the person who wrote this is utterly insane.
    • Chetan Sharma
      Chetan Sharma about 13 years
      It is just some class, I think to answer your questions, we must have some more code, where you actually declare the object of this class
  • rockerest
    rockerest about 13 years
    Be careful about your $dbh versus $this->dbh. They are completely different variables. What you said: >"$dbh can be used in any function inside the class" is actually wrong. $this->dbh can be used in any function, if it's been set somewhere. $dbh is a local variable just like any other.
  • wordpressquestion
    wordpressquestion about 13 years
    It is not inherited class. I'm sure.
  • jasir
    jasir over 9 years
    ...and this is terrible behaviour of Php (can be usefull sometimes, but...) Therefore - have a look at Nette\Object, which makes Php a more sane language doc.nette.org/en/2.2/php-language-enhancements
  • trejder
    trejder almost 9 years
    @jasir Sounds nearly like self-advertisement! :> Nearly every PHP framework out there makes PHP more sane language. That is the very key purpose of writing frameworks, after all. Can you name at least one PHP framework, which wasn't created for clearing pure PHP mad mess a little bit? Or that hasn't this objective as one of key reasons?
  • jasir
    jasir almost 9 years
    @trejder 1st) I am in no way connected to Nette Framework (except being very satisfied user 2nd) please point me to other framework base Object implementation, which gives you strict access, automatic properties & getters/setters and extension method. Thank you ...ps, current documentation, utils\object can be used alone now.
  • trejder
    trejder almost 9 years
    @jasir Yii2? (BTW: I am in no way connected to Yii2, except for being very satisfied user as well! :>)
  • jasir
    jasir almost 9 years
    @trejder Thanks, yes, then Yii2 object is also solution for this terrible php behaviour. Thanks for pointing out