在php中使用getter从父类调用受保护变量
我是PHP中OOP的新手。我有以下问题: 我有一个名为“User”的父类和两个子类“Business”和“Standaard” 我的父类看起来像这样:在php中使用getter从父类调用受保护变量,php,oop,variables,getter,Php,Oop,Variables,Getter,我是PHP中OOP的新手。我有以下问题: 我有一个名为“User”的父类和两个子类“Business”和“Standaard” 我的父类看起来像这样: class User { protected $m_iId; protected $m_iFoto; protected $m_sEmail; /*Constructor*/ public function __construct(){ $this->Id = -1; $this->Foto = "";
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;
}
}
孩子看起来像这样:
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;
}
}
我的问题如下:
如何申请身份证?
我想做一些类似的事情:
$oUser = new UserStandaard();
echo $oUser->Id;
但这不起作用。。。它一直在回响
Unknown property: Id
谢谢 这是因为在UserStandaard类中的重写
\uu get()
方法中没有Id
/*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;
}
但你可能会想,这是在他的父母身上
否,因为您已重写父方法
您试图在用户类中使用uuu get和uuu set的神奇方法重载属性,但覆盖了userstandaard类中的这些函数。这两个函数只从自己的类中知道自己的属性,因为Userstandaard的_get方法不调用它的父方法。但即使您这样做了,它也会失败,因为您是如何用开关案例和不同的属性名实现神奇方法的
因此,在UserStandaard中使用magic方法只知道1个属性,如果从User调用父方法,它只知道父属性(如Jon提到的)
我会说改变你的设计,这是行不通的
乔恩有一个很好的例子说明了它是如何工作的
哦,还有对你的一声呐喊:Welkom bij stack overflow!;) 问题是
\uuu get
和\uu set
的派生类实现不调用基类实现。这不是自动发生的,您需要明确执行:
class UserStandaard extends User {
/*Setter*/
public function __set($p_sProperty, $p_sValue){
parent::__set($p_sProperty, $p_sValue);
// your current code follows
}
}
但是,这仍然不起作用,因为现在您的父实现将只识别它知道的三个属性。似乎没有直接的方法来处理这个问题,这意味着您的设计有问题,需要修改
可能的解决办法
结构\uuu get
和\uu set
更加可插拔(示例待定)。我的意思是,它们识别的属性集不应该硬编码,因为这使得扩展它们非常困难。理想情况下,每个属性都需要不同的getter和setter:
class User {
public function getName() { ... }
public function setName($name) { ... }
}
这将使您保持代码的整洁和分离。然后,您可以连接\u get
和\u set
以转发到以下方法:
public function __get($name)
{
$getter = 'get'.ucfirst($name);
if (!method_exists($this, $getter)) {
die("This object does not have a $name property or a $getter method.");
}
return $this->$getter();
}
public function __set($name, $value)
{
$setter = 'set'.ucfirst($name);
if (!method_exists($this, $setter)) {
die("This object does not have a $name property or a $setter method.");
}
$this->$setter($value);
}
通过这种方式,您可以将每个属性分解为如上所述的getter和setter方法,而且绝对不需要在派生类中重写\uuuu get
和\uu set
。只需为您感兴趣的属性声明额外的getter和setter,基类\uuuu get
/\uu set
将正确地转发到这些方法