PHP Inheritance help
Posted: Mon May 10, 2010 1:50 pm
I am trying to mess with inheritance in PHP and was wondering why the code below doesn't work.
Test file
Parent class
Child Class
I am just trying to call the "printDbInfo" function in my parent class using the child class object created in the test file...but it doesn't print anything. I am assuming it's because it thinks $this->hostname is null...but I dont know how to get it to work.
Any help will be appreciated!
Test file
Code: Select all
$dbObj = new DbConn;
$dbObj->printDbInfo(); //displays xxxx
$test = new Amfphp();
$test->printDbInfo(); //Doesn't display anythingCode: Select all
class DbConn
{
public $hostname;
public $dbName;
public $username;
public $password;
//Constructor
public function __construct()
{
$this->hostname = "xxxx";
$this->dbName = "xxxx";
$this->username = "xxx";
$this->password = "xxx";
}
public function connect()
{
$db_link = mysql_connect($this->hostname, $this->username, $this->password);
mysql_select_db($this->dbName, $db_link);
}
public function printDbInfo()
{
print($this->hostname);
}
public function disconnect()
{
mysql_close();
}Code: Select all
class Amfphp extends DbConn
{
function __construct()
{
}
function getData()
{
print("test");
}
}Any help will be appreciated!