3

php の __get と __set の例は何千もありますが、残念ながら実際にそれらの使用方法を教えてくれる人は誰もいません。

したがって、私の質問は、オブジェクトを使用するときに、クラス内から __get および __set メソッドを実際に呼び出すにはどうすればよいかということです。

コード例:

class User{
public $id, $usename, $password;

public function __construct($id, $username) {
         //SET AND GET USERNAME
}

public function __get($property) {
    if (property_exists($this, $property)) {
        return $this->$property;
    }
}

public function __set($property, $value) {
    if (property_exists($this, $property)) {
        $this->$property = $value;
    }

    return $this;
}
}

$user = new User(1, 'Bastest');
// echo GET THE VALUE;

コンストラクターで値を設定する方法と、コンストラクターで値を取得する方法// echo GET THE VALUE;

4

1 に答える 1