From 67ec78157631f4d2eb9d737b5c689fd51b735880 Mon Sep 17 00:00:00 2001 From: icewing Date: Fri, 25 Apr 2008 15:57:58 +0000 Subject: Marcus Povey * Objects can be accessed as arrays git-svn-id: https://code.elgg.org/elgg/trunk@535 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'engine/lib/entities.php') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 3e912836a..21ef3d595 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -22,7 +22,8 @@ abstract class ElggEntity implements Exportable, // Allow export of data Importable, // Allow import of data - Iterator // Override foreach behaviour + Iterator, // Override foreach behaviour + ArrayAccess // Override for array access { /** * The main attributes of an entity. @@ -511,6 +512,37 @@ return $this->valid; } + // ARRAY ACCESS INTERFACE ////////////////////////////////////////////////////////// + /* + * This lets an entity's attributes be accessed like an associative array. + * Example: http://www.sitepoint.com/print/php5-standard-library + */ + + function offsetSet($key, $value) + { + if ( array_key_exists($key, $this->attributes) ) { + $this->attributes[$key] = $value; + } + } + + function offsetGet($key) + { + if ( array_key_exists($key, $this->attributes) ) { + return $this->attributes[$key]; + } + } + + function offsetUnset($key) + { + if ( array_key_exists($key, $this->attributes) ) { + $this->attributes[$key] = ""; // Full unsetting is dangerious for our objects + } + } + + function offsetExists($offset) + { + return array_key_exists($offset, $this->attributes); + } } /** -- cgit v1.2.3