1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/docs/en/cookbook/implementing-arrayaccess-for-domain-objects.rst

113 lines
3.1 KiB
ReStructuredText
Raw Normal View History

Implementing ArrayAccess for Domain Objects
===========================================
2010-04-06 22:36:40 +04:00
.. sectionauthor:: Roman Borschel (roman@code-factory.org)
This recipe will show you how to implement ArrayAccess for your
domain objects in order to allow more uniform access, for example
in templates. In these examples we will implement ArrayAccess on a
`Layer Supertype <http://martinfowler.com/eaaCatalog/layerSupertype.html>`_
for all our domain objects.
2010-04-06 22:36:40 +04:00
Option 1
--------
2010-04-06 22:36:40 +04:00
In this implementation we will make use of PHPs highly dynamic
nature to dynamically access properties of a subtype in a supertype
at runtime. Note that this implementation has 2 main caveats:
2010-04-06 22:36:40 +04:00
- It will not work with private fields
- It will not go through any getters/setters
2010-04-06 22:36:40 +04:00
.. code-block:: php
<?php
2010-04-06 22:36:40 +04:00
abstract class DomainObject implements ArrayAccess
{
public function offsetExists($offset) {
return isset($this->$offset);
}
2010-04-06 22:36:40 +04:00
public function offsetSet($offset, $value) {
$this->$offset = $value;
}
2010-04-06 22:36:40 +04:00
public function offsetGet($offset) {
return $this->$offset;
}
2010-04-06 22:36:40 +04:00
public function offsetUnset($offset) {
$this->$offset = null;
}
}
Option 2
--------
2010-04-06 22:36:40 +04:00
In this implementation we will dynamically invoke getters/setters.
Again we use PHPs dynamic nature to invoke methods on a subtype
from a supertype at runtime. This implementation has the following
caveats:
2010-04-06 22:36:40 +04:00
- It relies on a naming convention
- The semantics of offsetExists can differ
- offsetUnset will not work with typehinted setters
2010-04-06 22:36:40 +04:00
.. code-block:: php
2010-04-06 22:36:40 +04:00
<?php
2010-04-06 22:36:40 +04:00
abstract class DomainObject implements ArrayAccess
{
public function offsetExists($offset) {
// In this example we say that exists means it is not null
$value = $this->{"get$offset"}();
return $value !== null;
}
2010-04-06 22:36:40 +04:00
public function offsetSet($offset, $value) {
$this->{"set$offset"}($value);
}
2010-04-06 22:36:40 +04:00
public function offsetGet($offset) {
return $this->{"get$offset"}();
}
2010-04-06 22:36:40 +04:00
public function offsetUnset($offset) {
$this->{"set$offset"}(null);
}
}
Read-only
---------
You can slightly tweak option 1 or option 2 in order to make array
access read-only. This will also circumvent some of the caveats of
each option. Simply make offsetSet and offsetUnset throw an
exception (i.e. BadMethodCallException).
.. code-block:: php
2010-04-06 22:36:40 +04:00
<?php
2010-04-06 22:36:40 +04:00
abstract class DomainObject implements ArrayAccess
{
public function offsetExists($offset) {
// option 1 or option 2
}
2010-04-06 22:36:40 +04:00
public function offsetSet($offset, $value) {
throw new BadMethodCallException("Array access of class " . get_class($this) . " is read-only!");
}
2010-04-06 22:36:40 +04:00
public function offsetGet($offset) {
// option 1 or option 2
}
2010-04-06 22:36:40 +04:00
public function offsetUnset($offset) {
throw new BadMethodCallException("Array access of class " . get_class($this) . " is read-only!");
}
}