1

我是 PHP 中 OOP 的新手。我有以下问题:我有 1 个名为“用户”的父类和 2 个子类“业务”和“标准”。

我的父类看起来像这样:

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

谢谢!!

4

2 回答 2

2

The problem is that your derived class implementation of __get and __set do not call the base class implementation. This does not happen automatically, and you need to do it explicitly:

class UserStandaard extends User {

    /*Setter*/                    
    public function __set($p_sProperty, $p_sValue){
        parent::__set($p_sProperty, $p_sValue);
        // your current code follows
    }
}

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 __get and __set to 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:

class User {
    public function getName() { ... }
    public function setName($name) { ... }
}

This would let you keep the code nice, clean and separated. You can then wire up __get and __set to forward to these methods:

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);
}

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 __get and __set in your derived classes. Simply declare additional getters and setters for the properties you are interested in and the base class __get/__set will correctly forward to these methods.

于 2013-02-01T09:49:13.497 回答
1

__get()这是因为您的 UserStandaard 类中的覆盖方法中没有 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;
    }

但它在他的父母身上,你可能会想。

不,因为您已经覆盖了 parents 方法。

You are trying to overload properties with magic methods in your User class with __get and __set, but you override these functions in you UserStandaaard class. Both function only know their own properties from their own class because the Userstandaard's __get method, does not call it's parent method. But even if you do, it will fail because of the way how you implemented your magic methods with switch-cases and different property names.

So using the magic method in UserStandaard will only know 1 property, and if you call the parent method from User, it will only know the parents properties (like Jon mentioned).

I'd say change your design, this will not work.

Jon has an excellent example of how it could work!

Oh and a shoutout to you: Welkom bij stack-overflow ! ;)

于 2013-02-01T09:48:04.370 回答