0

I'm following the tutorial from zf2 website and at one point they create some properties:

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;
    }
}

they are public and if i make them protected then when i use them in my query i get an error saying that i can access them:

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

How can i keep them protected and access them in the Model Table (or Mapper)?

Any ideas?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • Please specify "`when i use them in my query`". Usually you'd write setter/getter methods. Alternatively for ZF2 specific purpose implementing `toArray()` or `getArrayCopy()` works pretty often – Sam Oct 12 '12 at 07:30
  • @Sam where can i find an example of using `toArray()` or `getArrayCopy()` in zf2? , this might be interesting if it works – Patrioticcow Oct 12 '12 at 21:36
  • See http://packages.zendframework.com/docs/latest/manual/en/user-guide/forms-and-actions.html - its used within hydrating strategies – Sam Oct 13 '12 at 08:24

3 Answers3

1

You need to modify the code to use setters and getters, which is good practice anyway:-

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
}

Then to access those properties:-

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

$albumId = $album->getId();
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • Getters and setters make sense only if there's logic that needs to be encapsulated in them. Since the validation logic is being performed elsewhere, an argument can be made that they are not necessary in this example. – weierophinney Oct 12 '12 at 14:00
  • @weierophinney He's asking how to access protected properties from outside the class. I'd be interested in learning how to do it without setters and getters. – vascowhite Oct 12 '12 at 15:27
  • My point was that unless there's a good reason to encapsulate them, public is fine. If you need to encapsulate them, the appropriate way is to use getters and setters -- and then, in this example, you'd switch to the ClassMethods hydrator instead of the ObjectProperty hydrator. – weierophinney Oct 12 '12 at 21:17
  • ah, OK. I see what you mean. I was coming from the point of view that the OP is a beginner following a tutorial and needed basic guidance, maybe I underestimated the OP. Thanks for the clarification. Nice work on ZF2 by the way :) – vascowhite Oct 12 '12 at 21:21
0

Add getters:

public function getId()
{
    return $this->Id;
}
GeeH
  • 563
  • 4
  • 14
0

I believe this tutorial leaves these properties as public so they can avoid implementing the magic methods __set() and __get(). Typically used in conjunction with mutators and accessors (setter and getter methods) to access protected and private properties in a class.

For Example:

/**
 * 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;
    }

The __get() would be similar but reversed.

RockyFord
  • 8,529
  • 1
  • 15
  • 21