1. 程式人生 > >魔術方法:__set、__get

魔術方法:__set、__get

<?php
class A{
    //用__set、__get魔術方法,欄位必須為私有屬性
    private $name = "herghost";
    public function display(){
        echo "displayA";
    }
    function __set($name,$value){
        $this->$name = $value."set";
    }
    function __get($name){
        return $this->$name;
    }
}


$a = new
A(); $a->name = "ac"; echo $a->name; ?>
結果:acset