Jump to content

OOP Variable Problems


ILMV

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/137934-oop-variable-problems/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.