0

同じプロパティの再帰制限__getをオーバーライドする方法はありますか。__set最初のエントリとは異なる方法で 2 回目の再エントリを処理できるようにしたいと考えています。

このコード例は実用的ではありませんが、最も簡単に説明できます。

 class Foo {
     public function __set($name,$value){
         print "$name entered\n";
         this->$name = $value; // want it to recurse here
     }
 }

$a = new Foo();
$a->baz = "derp";
print $a->baz;

// should get (cannot test at the moment)
// baz entered
// derp  <- you get derp because the current php implementation creates an instance variable from the second call to __set

インターネットがダウンしているため、携帯電話で入力しているため、タイプミスが発生する可能性があります。

4

2 に答える 2

0

これは古い質問だと思いますが、これはあなたが実際に探していたものだと思います.

<?php
class Foo {
    private $_data = array();
    public function __set($name,$value){
        print "$name entered\n";
        $this->_data[$name] = $value;
    }
    public function __get($name){
        if(array_key_exists($name, $this->_data)){
            return $this->_data[$name];
        } else {
            return false;
        }
    }
 }

$a = new Foo();
$a->baz = "derp";
print $a->baz;
?>

http://phpfiddle.org/main/code/4h0-an7

于 2013-07-30T11:20:27.953 に答える
0

その構文でこれを行う方法はありません。__set代わりに直接呼び出すだけです。

class Foo {
     public function __set($name, $value) {
         print "$name entered\n";

         $this->__set($name, $value);
     }
}
于 2012-08-21T14:59:22.527 に答える