I am trying to access the variables from the parent class as shown below:
//PARENT CLASS
class InfoString{
private $username = "JOE";
public function _construct(){}
protected function get_username(){
return $this->username;
}
}
class Service extends InfoString{
//this class should now inherit the variables in InfoString, right??
public function _construct(){}
public function hello_username(){
echo "HELLO! ". parent::get_username();
}
}
and I call the class like so:
$a = new Service();
$a->hello_username(); //prints nothing, instead of the username
Instead of getting “HELLO! JOE”, I get an empty string. What am I doing wrong here?
Also, suppose the class ‘InfoString’ will contain configuration parameters – is it a good idea to extend this class, or what would be the proper implementation to get the config variables from, say class ‘InfoString’ into another class???
Thanks.
You’re just extending
InfoString, so you can just use$this->get_username();and it will work fine.::is for calling static methods.Also, you’d get an error if you’d increase your error level. Call
error_reporting(E_ALL); this will give you clues to solve similar issues in the future.