【问题标题】:how to pass protected variables to the table model (or mapper) in Zend Framework 2?如何将受保护的变量传递给 Zend Framework 2 中的表模型(或映射器)?
【发布时间】:2012-10-02 23:27:20
【问题描述】:

我正在关注tutorial from zf2 website,他们在某一时刻创建了一些属性:

namespace Album\Model;

class Album
{
    public $id;
    public $artist;
    public $title;

    public function exchangeArray($data)
    {
        $this->id     = (isset($data['id'])) ? $data['id'] : null;
        $this->artist = (isset($data['artist'])) ? $data['artist'] : null;
        $this->title  = (isset($data['title'])) ? $data['title'] : null;
    }
}

它们是public,如果我将它们设为protected,那么当我在查询中使用它们时,我会收到一条错误消息,提示我可以访问它们:

cannot access protected property Album\Model\Album::$artist

我怎样才能保留它们protected 并在模型表(或映射器)中访问它们?

有什么想法吗?

【问题讨论】:

  • 请指定“when i use them in my query”。通常你会编写 setter/getter 方法。或者,对于 ZF2 特定目的,实现 toArray()getArrayCopy() 经常工作
  • @Sam 我在哪里可以找到在 zf2 中使用 toArray()getArrayCopy() 的示例? ,如果可行,这可能会很有趣

标签: php variables zend-framework2 protected


【解决方案1】:

您需要修改代码以使用 setter 和 getter,无论如何这是一个好习惯:-

namespace Album\Model;

class Album
{
    protected $id;
    protected $artist;
    protected $title;

    public function exchangeArray($data)
    {
        $this->id     = (isset($data['id'])) ? $data['id'] : null;
        $this->artist = (isset($data['artist'])) ? $data['artist'] : null;
        $this->title  = (isset($data['title'])) ? $data['title'] : null;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }
    //You get the idea for the rest, I'm sure
}

然后访问这些属性:-

$album = new Album();
$album->setId(123);

$albumId = $album->getId();

【讨论】:

  • 只有在需要封装逻辑的情况下,Getter 和 setter 才有意义。由于验证逻辑是在其他地方执行的,因此可以认为在此示例中它们不是必需的。
  • @weierophinney 他在问如何从类外访问受保护的属性。我有兴趣学习如何在没有 setter 和 getter 的情况下做到这一点。
  • 我的意思是,除非有充分的理由封装它们,否则公开是可以的。如果您需要封装它们,适当的方法是使用 getter 和 setter——然后,在本例中,您将切换到 ClassMethods 水合器而不是 ObjectProperty 水合器。
  • 啊,好的。我明白你的意思了。我的观点是 OP 是一个初学者,遵循教程并需要基本指导,也许我低估了 OP。感谢您的澄清。顺便说一句,在 ZF2 上做得很好 :)
【解决方案2】:

添加吸气剂:

public function getId()
{
    return $this->Id;
}

【讨论】:

    【解决方案3】:

    我相信本教程将这些属性保留为公开,因此它们可以避免实现 magic methods __set() and __get()。通常与 mutator 和 accessors(setter 和 getter 方法)结合使用,以访问类中的受保护和私有属性。

    例如:

    /**
     * Map the setting of non-existing fields to a mutator when
     * possible, otherwise use the matching field
     * 
     *  $object->property = $value; will work the same as
     *  $object->setProperty($value);
     */
    public function __set($name, $value)
        {
    
            $property = strtolower($name);
    
            if (!property_exists($this, $property)) {
                throw new \InvalidArgumentException("Setting the property '$property'
                        is not valid for this entity");
            }
            $mutator = 'set' . ucfirst(strtolower($name));
    
            if (method_exists($this, $mutator) && is_callable(array($this, $mutator))) {
                $this->$mutator($value);
            } else {
                $this->$property = $value;
            }
    
    
            return $this;
        }
    

    __get() 类似但相反。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2014-07-04
      相关资源
      最近更新 更多