PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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();
}
class Amfphp extends DbConn
{
function __construct()
{
}
function getData()
{
print("test");
}
}
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.
If you don't define a __construct() in the child, then the one in the parent will be called. However, once you define a __construct() in the child then you will need to manually call the parent. This allows you to totally override the parent _construct() if you want. So if you don't need it in the child then don't define it. If you do need it, then do this:
class Amfphp extends DbConn
{
function __construct()
{
parent::__construct();
}
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.