Purpose Of Magic Getters / Setters

I thought about the getters / setters that are used pretty often in Yii.

However I think, that the advantages are not that big in compare to the disadvantages.


  • clean API

  • performance better when having many public attributes in compare to using always a setter / getter method and private attributes

  • useful in CActiveRecord to create attributes on the fly


  • references are harder to realize (maybe possible by a condition in __set(), getting a reference is probably still harder or impossible)

  • inheritance: you can’t use a custom setter / getter method in a subclass, if you don’t use it in the parent class, because the visibility has to be at least as high as in the parent class (alternative: make attributes always private / protected, but then the pro argument concerning performance disappears)

  • even if you make the attribute private / protected and then you access it from a method inside a class the magic methods won’t be called (you would have to name these attributes different from the name that you use for accessing them to invoke the magic --> not nice)



class parent


    public $attribute;

    private $priv;

    public function getPriv() // will be invoked when accessing the property from outside, but not inside


        //do something

        return $this->priv;


    public function calc()


        echo $this->priv; // getPriv() won't be invoked



class child extends parent


    //protected $attribute; not possible

    public function setAttribute($value) // won't be invoked --> uselesss


        // do some checks

        $this->attribute = $value;




So outside from CActiveRecord I can’t find good reasons for using the magic instead of usual getter and setter methods with private / protected attributes.

As I have already mentioned for some problems you can find workarounds, but they are not really nice and can slow down the performance.

Maybe someone can tell me the reason for the intensive use of it?

(in this post attribute stands for the property of a class, NOT just an AR’s attribute)