27

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?

NineCattoRules
  • 2,253
  • 6
  • 39
  • 84
wordpressquestion
  • 745
  • 1
  • 7
  • 18
  • 1
    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. – rockerest Mar 31 '11 at 01:56
  • 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 – Chetan Sharma Mar 31 '11 at 02:31

6 Answers6

23

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

}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
wordpressquestion
  • 745
  • 1
  • 7
  • 18
18

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
Unsigned
  • 9,640
  • 4
  • 43
  • 72
  • ...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 http://doc.nette.org/en/2.2/php-language-enhancements – jasir Sep 16 '14 at 07:32
  • 1
    @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? – trejder Jul 02 '15 at 12:06
  • @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. – jasir Jul 08 '15 at 07:43
  • @jasir Yii2? (BTW: I am in no way connected to Yii2, except for being very satisfied user as well! :>) – trejder Jul 08 '15 at 10:23
  • 1
    @trejder Thanks, yes, then Yii2 object is also solution for this terrible php behaviour. Thanks for pointing out – jasir Jul 08 '15 at 10:35
2
  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.
emco
  • 4,589
  • 3
  • 18
  • 20
  • 2
    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. – rockerest Mar 31 '11 at 02:03
1

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
Chetan Sharma
  • 2,539
  • 5
  • 25
  • 41
1

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

Icode4food
  • 8,504
  • 16
  • 61
  • 93
0
class foo {
  function select($p1, $dbh=null) {
    if ( is_null($dbh) )
        $this->dbh = $dbh ; 
    return; 
  }

  function get() {
    return $this->dbh; 
  }
}
bstpierre
  • 30,042
  • 15
  • 70
  • 103