I’m a newbie in OOP in PHP. I have the following problem:
I have 1 parent class called ‘User’ and 2 subclasses ‘Business’ and ‘Standaard’.
My parent class looks something like this:
class User {
protected $m_iId;
protected $m_iFoto;
protected $m_sEmail;
/*Constructor*/
public function __construct(){
$this->Id = -1;
$this->Foto = "";
$this->Email = "";
}
/*Setter*/
public function __set($p_sProperty, $p_sValue){
switch($p_sProperty){
case "Id":
if(is_numeric($p_sValue) && $p_sValue !== -1){
$iNumber = intVal($p_sValue);
if($iNumber >= -1){
$this->m_iId = $p_sValue;
}
else{
echo("Not a valid id: ".$p_sValue);
}
}
break;
case "Foto":
if(is_numeric($p_sValue) && $p_sValue !== -1){
$iNumber = intVal($p_sValue);
if($iNumber >= -1){
$this->m_iFoto = $p_sValue;
}
else{
echo("Not a valid Foto_id: ".$p_sValue);
}
}
break;
case "Email":
$this->m_sEmail = $p_sValue;
break;
default: echo("Unknown property: ".$p_sProperty);
}
}
/*Getter*/
public function __get($p_sProperty){
$vResult = null;
switch($p_sProperty){
case "Id";
$vResult = $this->m_iId;
break;
case "Foto";
$vResult = $this->m_iFoto;
break;
case "Email";
$vResult = $this->m_sEmail;
break;
default: echo("Unknown property: ".$p_sProperty);
}
return $vResult;
}
}
A child child looks likes this:
class UserStandaard extends User {
//velden die bereikbaar zijn
protected $m_sName;
/*Constructor*/
public function __construct(){
parent::__construct();
$this->Name = "";
}
/*Setter*/
public function __set($p_sProperty, $p_sValue){
switch($p_sProperty){
case "Name":
$this->m_sName = $p_sValue;
break;
default: echo("Unknown property: ".$p_sProperty);
}
}
/*Getter*/
public function __get($p_sProperty){
$vResult = null;
switch($p_sProperty){
case "Name";
$vResult = $this->m_sName;
break;
default: echo("Unknown property: ".$p_sProperty);
}
return $vResult;
}
}
My question is the following:
How can I request the Id?
I want to do something like:
$oUser = new UserStandaard();
echo $oUser->Id;
But that doesn’t work… It keeps echoing
Unknown property: Id
Thanks!!
The problem is that your derived class implementation of
__getand__setdo not call the base class implementation. This does not happen automatically, and you need to do it explicitly:However, this will still not work because now your parent implementation will only recognize the three properties that it knows. There seems to be no direct way to handle this, which means your design is problematic and needs to be modified.
Possible solution
Structure
__getand__setto be more pluggable (example pending). By this I mean that the set of properties they recognize should not be hardcoded because this makes it very difficult to extend them. Ideally, you would want a different getter and setter for each property:This would let you keep the code nice, clean and separated. You can then wire up
__getand__setto forward to these methods:This way you can break down each property into a getter and setter method as above, and also there is absolutely no need to override
__getand__setin your derived classes. Simply declare additional getters and setters for the properties you are interested in and the base class__get/__setwill correctly forward to these methods.