Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中重写magic setter和getter之后的参数_Php_Getter Setter_Magic Methods_Mapper - Fatal编程技术网

如何在php中重写magic setter和getter之后的参数

如何在php中重写magic setter和getter之后的参数,php,getter-setter,magic-methods,mapper,Php,Getter Setter,Magic Methods,Mapper,现在我有一个BaseObject,它对DB执行ORM。我依靠private$data和magicsetter和getter动态地创建一个对象,其中包含一堆列作为私有对象成员。在子类中,如果我想更改行为来设置单个对象成员,我必须绕过父setter并查找键。我的问题是,是否有更好的方法来实现这一点,我可以只覆盖单个对象成员,而不是通过设置器 基本对象映射器,映射到db并动态创建一组私有参数 class Base { private $data = array(); public

现在我有一个BaseObject,它对DB执行ORM。我依靠private$data和magicsetter和getter动态地创建一个对象,其中包含一堆列作为私有对象成员。在子类中,如果我想更改行为来设置单个对象成员,我必须绕过父setter并查找键。我的问题是,是否有更好的方法来实现这一点,我可以只覆盖单个对象成员,而不是通过设置器

基本对象映射器,映射到db并动态创建一组私有参数

class Base
 {
    private $data = array();

    public function __construct(){
        //get columns info from db and do set
    }
    public function __set($key, $value){
        $this->data[$key] = $value;        
    }

   public function __get($key){
       return isset($this->data[$key])? $this->data[$key] : null;       
   }
}
还有儿童班。现在要覆盖参数设置,我必须这样做

 class Child extends Base
 {
     public function __set($name, $value){
        if($name == 'dog' && $value == 'chihuahua')
          $this->dog = 'dog bark wolf wolf';
        else if($name == 'cat' && $value == 'sand')
          $this->cat = 'cat say meow meow';
       ...
     }
 }
我的问题是,有没有一种优雅的方法可以做到这一点,也许在儿童课堂上也有类似的方法

public function __set_dog($value)
    $this->dog = 'dog bark wolf wolf';

public function __set_cat($value)
   $this->cat = 'cat say meow meow';
目标是

$animal1 = new Animal();
$anmial1->dog = 'pit bull';
$anmial1->cat= 'sand';

$animal2 = new Animal();
$anmial1->dog = 'chihuahua';
$anmial1->cat= 'house cat';

您可以动态检查传统命名的getter/setter方法是否存在,并调用它,而不是返回$data成员。下面是一个例子:

class Base {
    public function __get($key) {
        if (method_exists($this, $method = 'get' . ucfirst($key))) {
           return $this->$method($key);
        } else {
           return isset($this->data[$key])? $this->data[$key] : null;
        }
    }

    public function __set($key, $value) {
        if (method_exists($this, $method = 'set' . ucfirst($key))) {
           $this->$method($key, $value);
        } else {
           $this->data[$key] = $value;
        }
    }
}

class Animal extends Base {
    protected $data = array('fish' => 'blublublub', 'dog' => 'woof', 'cat' => 'meow');

    public function getDog() {
        return 'dog bark wolf wolf';
    }

    public function getCat() {
        return 'cat say meow meow';
    }
}

$animal = new Animal();
echo $animal->fish; // "blublublub"
echo $animal->dog; // "dog bark wolf wolf"
echo $animal->cat; // "cat say meow meow"

您可以动态检查传统命名的getter/setter方法是否存在,并调用它,而不是返回$data成员。下面是一个例子:

class Base {
    public function __get($key) {
        if (method_exists($this, $method = 'get' . ucfirst($key))) {
           return $this->$method($key);
        } else {
           return isset($this->data[$key])? $this->data[$key] : null;
        }
    }

    public function __set($key, $value) {
        if (method_exists($this, $method = 'set' . ucfirst($key))) {
           $this->$method($key, $value);
        } else {
           $this->data[$key] = $value;
        }
    }
}

class Animal extends Base {
    protected $data = array('fish' => 'blublublub', 'dog' => 'woof', 'cat' => 'meow');

    public function getDog() {
        return 'dog bark wolf wolf';
    }

    public function getCat() {
        return 'cat say meow meow';
    }
}

$animal = new Animal();
echo $animal->fish; // "blublublub"
echo $animal->dog; // "dog bark wolf wolf"
echo $animal->cat; // "cat say meow meow"

你能在你想要达到的目标上多花点时间吗?我完全不知道你想做什么。现在我有一个BaseObject,它对数据库执行ORM。我依靠private$data和magicsetter和getter动态地创建一个对象,其中包含一堆列作为私有对象成员。在子类中,如果我想更改行为来设置单个对象成员,我必须绕过父setter并查找键。我的问题是,是否有更好的方法来实现这一点,我可以只覆盖单个对象成员,而不是通过设置器。提前感谢如果对象成员是私有的,不,你没有别的办法了。你的if语句使用的是=而你很可能的意思是==。nickb谢谢你捕捉到这一点,我只是在写sudo代码。谢谢。你能多花点时间谈谈你想要达到的目标吗?我完全不知道你想做什么。现在我有一个BaseObject,它对数据库执行ORM。我依靠private$data和magicsetter和getter动态地创建一个对象,其中包含一堆列作为私有对象成员。在子类中,如果我想更改行为来设置单个对象成员,我必须绕过父setter并查找键。我的问题是,是否有更好的方法来实现这一点,我可以只覆盖单个对象成员,而不是通过设置器。提前感谢如果对象成员是私有的,不,你没有别的办法了。你的if语句使用的是=而你很可能的意思是==。nickb谢谢你捕捉到这一点,我只是在写sudo代码。谢谢