Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Variable not available? Scope issue?

$
0
0

I didn't right this code but I can't figure out why the $db var isn't available in the DBPersister() constructor and in the functionName().

$s = new somePersister();
print_r($s->functionName(123)); 
class somePersister extends DBPersister {

  public function functionName($id) {
        ... do stuff ...
        print_r($this->db); //DOES NOT print DB instance
  }
}
<?php
include_once('db.inc.php');

print_r($db); //prints the DB instance

class DBPersister {

    var $db;

    function DBPersister()  {
        global $db;
        print_r($db); //DOES NOT print the DB instance
        $this->db =& $db;
    }

}

db.inc.php

class DB extends _Database_ {
    var $host;
    var $dbname;
    var $user;
    var $password;
    var $result;
    var $record;
    var $connected;

    function DB() {
        $this->_Database_();
        $this->host     = DATABASE_HOST;
        $this->dbname   = DATABASE_NAME;
        $this->user     = DATABASE_USERNAME;
        $this->password = DATABASE_PASSWORD;
    }
}

$db = new DB();

Viewing all articles
Browse latest Browse all 13200

Trending Articles