PHP $this variable

前端 未结 6 1274
日久生厌
日久生厌 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

    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.

提交回复
热议问题