I have a base class and a a second class that’s extends the base class. The problem is that I can’t access the $db object from the child class, but all normal variables.
Any idea why?
class Base_Login
{
protected $member_uri = 'member.php';
protected $db;
public function __construct($db_user, $db_password, $db_name, $db_host)
{
$this->db = new ezSQL_mysql($db_user, $db_password, $db_name, $db_host);
var_dump($this->db); //Output: Object content's
}
}
class Facebook_Login extends Base_Login
{
public function __construct($app_id, $secret)
{
var_dump($this->db); //Output: NULL
echo $this->member_uri //Output: member.php
}
}
And this is how i call it:
require("includes/config.php");
$base_login = new Base_Login($db_user,$db_password,$db_name,$db_host);
$base_login->Facebook_Login = new Facebook_Login($facebook_appID,$facebook_secret);
In your parent class you have defined a constructor method. and the initialization of the database object is done in your parent class. when you define a child class and define the method with same name as parent’s what php does here is override the parent’s method will child so for example
and now you have another class inheriting the parent class
what php does here is ignore the parent’s method and call only the child class. workarond for this is to call your parent’s method in your child class by using parent keyword
so in your child class
this will result in the parent’s as well as child method being executed. same goes for the class constructor. you need to class parent constructor method in your child class by using
parent::__construct();hope this helps you in understanding how php handles method overriding.