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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.