Howdy all...
I'm going to take the weekend's quiteness for something of a concensus here. Over the weekend I put together what I believe is the final interface. The last posts we had here seemed to lean toward naming it Keyed, so that's what I've gone with. Short, and shouldn't class with any object names.
Here is the highlight reel:
PHP Code:
interface Keyed {
public function __get($key);
public function __set($key, $value);
public function __isset($key);
public function __unset($key);
}
// attempt a __get('key') when __isset('key') == false
class NotFoundException extends RuntimeException { }
// attempt __set()/__unset() when not a allowed
class ReadOnlyException extends RuntimeException { }
// attempt any method, but generally __unset(), when not allowed
class IllegalOperationException extends RuntimeException{ }
This gives us all of the magic methods, and expected exceptions to throw whenever the object we want to implement doesn't allow the behaviors that have been mentioned here. Thoughts, comments? If there aren't any, I'll put together a package and upload it to SF over the next day or so.
To view the current code, and the file structure I'm using, you can look at the repository at: https://svn.opensir.org/svn/trunk/src/
Finally, here is the full interface, comments and all:
PHP Code:
<?php
/**
* This file contains {@link Keyed}.
*
* @package OpenSIR
*/
/**
* Setup OpenSIR environment variables.
*/
require_once dirname(__FILE__) . '/opensir.library.php';
/**
* Provides an interface for storing and retrieving any number of "Keyed"
* properties via PHP's {@link http://us2.php.net/manual/en/language.oop5.magic.php magic
* methods}.
*
* This interfaces is meant to be used by any object that should expose values
* to match a given key. Specific implementations are left up to the developer,
* but they must provide for catching the requests to get or set via the magic
* method __get()/__set(). In addition, __isset() is specified. In PHP 5.1,
* this is used by the internal isset() to insure that a property is set. The
* code would determine if a property is set in PHP >= 5.1 is
* <i>isset($keyed->key)</i>, in PHP < 5.1, however, it would be
* <i>$keyed->__isset('key')</i>. Both function identically, but only the
* latter is compatible with versions of PHP 5.0.5 and lower.
*
* Present in this specification are {@link __set()} and {@link __unset()}.
* When implementing Keyed in a read-only environment, it is acceptable to throw
* a {@link ReadOnlyException} for both of these methods. In an environment
* where {@link __set()} can be called, but a value can not be completely
* removed via {@link __unset()}, it is acceptable to throw a
* {@link IllegalOperationException}.
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @author Collectively authored via
* {@link http://www.sitepoint.com/forums/showthread.php?t=298611}.
*/
interface Keyed
{
/**
* Retrieves a mixed value from this container by its given <i>$key</i>.
*
* Should throw {@link NotFoundException} if <i>__isset($key) == FALSE</i>.
*
* @param string
* @return mixed
*/
public function __get($key);
/**
* Sets the property of a given <i>$key</i> to a given <i>$value</i>.
*
* May throw {@link ReadOnlyException} if this object is read-only.
*
* @param string
* @param mixed
*/
public function __set($key, $value);
/**
* Returns <i>TRUE</i> if a given property is set, <i>FALSE</i> otherwise.
*
* @param string
* @return boolean
*/
public function __isset($key);
/**
* Removes a given value at <i>$key</i> from this object
*
* May throw {@link ReadOnlyException} if this object is read-only or
* {@link IllegalOperationException} if the <i>$key</i> can not be removed.
*
* @param string
*/
public function __unset($key);
}
Bookmarks