2

我最近研究了魔法方法,__get__set,并且想知道如何在类中实际设置和获取多个属性。

我知道它只适用于一个变量或数组,但我不确定访问多个变量。

有谁能给我解释一下吗?

class myMagic2 {
    public $data;
    public $name;
    public $age;

    public function __set($item, $value) {
        $this->item = $value;
    }

    public function __get($item){
        return $this->item;
    }
}

有没有办法访问所有变量($data, $name, $age)?

4

2 回答 2

3

当我在项目中工作时,我总是有这些方法:

public function __set($name, $value) 
{
    //see if there exists a extra setter method: setName()
    $method = 'set' . ucfirst($name);
    if(!method_exists($this, $method))
    {
        //if there is no setter, receive all public/protected vars and set the correct one if found
        $vars = $this->vars;
        if(array_search("_" . $name, $vars) !== FALSE)
            $this->{"_" . $name} = $value;
    } else
        $this->$method($value); //call the setter with the value
}

public function __get($name) 
{
    //see if there is an extra getter method: getName()
    $method = 'get' . ucfirst($name);
    if(!method_exists($this, $method)) 
    {
        //if there is no getter, receive all public/protected vars and return the correct one if found
        $vars = $this->vars;
        if(array_search("_" . $name, $vars) !== FALSE)
            return $this->{"_" . $name};
    } else
        return $this->$method(); //call the getter
    return null;
}

public function getVars()
{
    if(!$this->_vars)
    {
        $reflect = new ReflectionClass($this);
        $this->_vars = array();
        foreach($reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED) as $var)
        {
            $this->_vars[] = $var->name;
        }
    }
    return $this->_vars;
}

因此,如果我想在写入/返回之前操作它们,我可以自由地为属性创建额外的 setter/getter。如果属性不存在 setter/getter,它会退回到属性本身。使用 getVars() 方法,您可以从类中接收所有公共和受保护的属性。

我的类属性总是用下划线定义的,所以你可能应该改变它。

于 2013-07-17T15:39:07.637 回答
0

你可以遵循这个模式。

注意:在帖子中的示例中,不会调用魔术方法$obj->data$obj->name或者$obj->age因为这些值已经作为公共属性访问。我将它们更改为受保护并更改名称以隐藏它们。

<?php
class myMagic2{
    protected $_data;
    protected $_name;
    protected $_age;

    public function __set($item, $value){
        switch($item){
            case "data":
                $this->_data = $value;
                break;
            case "name":
                $this->_name = $value;
                break;
            case "age":
                $this->_age = $value;
                break;
            default:
                throw new Exception("Property $name does not exist.");

        }

    }

    public function __get($item){
        switch($item){
            case "data":
                return $this->_data;
                break;
            case "name":
                return $this->_name;
                break;
            case "age":
                return $this->_age;
                break;
            default:
                throw new Exception("Property $name does not exist.");
                break;

        }
    }
}

通常你会做更多的事情,然后使用魔法方法作为代理来设置和获取类属性。你会这样验证或过滤或以其他方式增加操作。如果您只是要获取或设置一个属性,您不妨将属性公开并放弃使用魔术方法。

于 2013-07-17T15:36:45.643 回答