1,10 → 1,40 |
<?php |
|
namespace de\pointedears; |
namespace PointedEars\PHPX; |
|
/** |
* Base class providing a generic getter and setter |
* Base class providing generic wrappers for reading from and |
* and writing to inaccessible properties. |
* |
* For each such property there must exist a public dynamically |
* bound method (the PHP default) in the inheriting class whose |
* name is prefixed with 'get' (a <i>getter</i>, for read access) |
* and/or with 'set' (a <i>setter</i>, for write access) followed |
* by the property name. (It is recommended to write the first |
* letter of the property name in the method name in uppercase. |
* For runtime-efficiency, underscores in the property name are |
* <em>not</em> converted to camel-case ["property_name" requires |
* "getProperty_name", <em>not</em> "getPropertyName"].) |
* |
* The getter or setter would then access the non-public property |
* whose name is the name of the accessed property prefixed with |
* underscore ('_'), called the <em>underscore property</em>. |
* It can make sure that the value of the underscore property is |
* of a specific type or within a specific range, i. e. perform |
* automatic type conversion, normalize values that are out of |
* range when the property is read, and reject/ignore attempts |
* to set unsuitable property values. This is particularly useful |
* with instances of model classes; see {@link AbstractModel} and |
* {@link Model}. |
* |
* Properties that do not have a getter are not available |
* from unprivileged context, and an exception is thrown |
* when attempting to read from them there. |
* |
* Properties that do not have a setter are effectively |
* <em>read-only</em> from unprivileged context, and |
* an exception is thrown when attempting to write to them there. |
* |
* @author Thomas 'PointedEars' Lahn |
*/ |
abstract class Base |
12,12 → 42,15 |
/** |
* Retrieves a property value. |
* |
* |
* @param string $name |
* Property name |
* @throws InvalidArgumentException |
* if the property does not exist or has no getter |
* if the underscore property for the property |
* named <code><var>$name</var></code> does not exist |
* or has no getter |
* @return mixed |
* Property value |
* Return value of the property-specific getter |
*/ |
public function __get ($name) |
{ |
29,21 → 62,26 |
|
if (property_exists($this, "_$name")) |
{ |
throw new \InvalidArgumentException("Property '_$name' has no getter"); |
throw new \InvalidArgumentException("Property '{$name}' has no getter"); |
} |
|
throw new \InvalidArgumentException("No such property: '_$name'"); |
throw new \InvalidArgumentException("No such property: '{$name}'"); |
} |
|
/** |
* Sets a property value. |
* |
* Called when attempting to write data to inaccessible |
* (usually protected or private) properties. |
* |
* @param string $name |
* Property name |
* @param mixed $value |
* Property value |
* @throws InvalidArgumentException |
* if the property does not exist or is read-only |
* if the protected underscore property for the property |
* named <code><var>$name</var></code> does not exist |
* or has no setter (is read-only) |
* @return mixed |
* Return value of the property-specific setter |
*/ |
57,9 → 95,9 |
|
if (property_exists($this, "_$name")) |
{ |
throw new \InvalidArgumentException("Property '_$name' has no setter"); |
throw new \InvalidArgumentException("Property '{$name}' has no setter"); |
} |
|
throw new \InvalidArgumentException("No such property: '_$name'"); |
throw new \InvalidArgumentException("No such property: '{$name}'"); |
} |
} |