ILMV Posted December 21, 2008 Share Posted December 21, 2008 Hello all, I am new to OOP, and have a problem with variable scope. Here is my code, certain parts have been removed to simplify the situation... <?php class config { public $title_default = 'Meltdown Tech'; public $host = ''; public $user = ''; public $password = ''; public $database = ''; } class db extends config { private static $queryCount = 0; public function __construct() { $conn = mysql_connect($this->host, $this->user, $this->password); $error="No errors 1"; if (!$conn) { $error="Unable to connect to DB: " . mysql_error(); exit; } $error="No errors 2"; if (!mysql_select_db($this->database)) { $error="Unable to select mydbname: " . mysql_error(); exit; } $error="No errors 3"; } public static function getQueryCount() { return self::$queryCount; } public function query($query) { ++self::$queryCount; return mysql_query($query); } } class base extends db { function constructTitle($title='') { if($title=='') { return $this->title_default; } else { return $this->$title_prefix . $title . $this->title_suffix; // <-- this is line 60 } } } ?> in db->__construct it can get the variables from config, but from base->constructTitle it returns an error.... 'Fatal error: Cannot access empty property in \class\base.class.php on line 60' I have tried to use public, private, protected for the variable scope on config, but none of them work. Any help much appreciated. ILMV Quote Link to comment Share on other sites More sharing options...
Sagi Posted December 21, 2008 Share Posted December 21, 2008 * Change $this->$title_prefix to $this->title_prefix, you don't have variable $title_prefix. * You didn't declare property "table_prefix" and "title_suffix" in class base or super classes (db, config). Quote Link to comment Share on other sites More sharing options...
ILMV Posted December 21, 2008 Author Share Posted December 21, 2008 NOOOOOOOOOOOOOOOOOOOOOOO!!! Ok so I cannot even see simple errors Thanks! Cheers sagi Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.