【发布时间】: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