Rein Van Leirsberghe
Rein Van Leirsberghe

Reputation: 765

Calling protected variable from parent class with getters in php

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!!

Upvotes: 1

Views: 2173

Answers (2)

Timmetje
Timmetje

Reputation: 7694

It's because you don't have Id in the overriding __get() method, in your UserStandaard class.

 /*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;
    }

But it is in his parent, you might think.

No because you have overridden the parents method.

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

Upvotes: 1

Jon
Jon

Reputation: 437336

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.

Upvotes: 2

Related Questions