note 113865 added to class.arrayaccess
| From: | YousefIsmaeilCliprz | Date: | Wed, 11 Dec 2013 07:38:26 +0000 |
| Subject: | note 113865 added to class.arrayaccess | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197902@lists.php.net to get a copy of this message | ||
<?php
/**
* ArrayAndObjectAccess
* Yes you can access class as array and the same time as object
*
* @author Yousef Ismaeil <cliprz@gmail.com>
*/
class ArrayAndObjectAccess implements ArrayAccess {
/**
* Data
*
* @var array
* @access private
*/
private $data = [];
/**
* Get a data by key
*
* @param string The key data to retrieve
* @access public
*/
public function &__get ($key) {
return $this->data[$key];
}
/**
* Assigns a value to the specified data
*
* @param string The data key to assign the value to
* @param mixed The value to set
* @access public
*/
public function __set($key,$value) {
$this->data[$key] = $value;
}
/**
* Whether or not an data exists by key
*
* @param string An data key to check for
* @access public
* @return boolean
* @abstracting ArrayAccess
*/
public function __isset ($key) {
return isset($this->data[$key]);
}
/**
* Unsets an data by key
*
* @param string The key to unset
* @access public
*/
public function __unset($key) {
unset($this->data[$key]);
}
/**
* Assigns a value to the specified offset
*
* @param string The offset to assign the value to
* @param mixed The value to set
* @access public
* @abstracting ArrayAccess
*/
public function offsetSet($offset,$value) {
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
/**
* Whether or not an offset exists
*
* @param string An offset to check for
* @access public
* @return boolean
* @abstracting ArrayAccess
*/
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
/**
* Unsets an offset
*
* @param string The offset to unset
* @access public
* @abstracting ArrayAccess
*/
public function offsetUnset($offset) {
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
}
}
/**
* Returns the value at specified offset
*
* @param string The offset to retrieve
* @access public
* @return mixed
* @abstracting ArrayAccess
*/
public function offsetGet($offset) {
return $this->offsetExists($offset) ? $this->data[$offset] : null;
}
}
?>
Usage
<?php
$foo = new ArrayAndObjectAccess();
// Set data as array and object
$foo->fname = 'Yousef';
$foo->lname = 'Ismaeil';
// Call as object
echo 'fname as object '.$foo->fname."\n";
// Call as array
echo 'lname as array '.$foo['lname']."\n";
// Reset as array
$foo['fname'] = 'Cliprz';
echo $foo['fname']."\n";
/** Outputs
fname as object Yousef
lname as array Ismaeil
Cliprz
*/
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/class.arrayaccess.php
Edit -- https://master.php.net/note/edit/113865
Del: integrated -- https://master.php.net/note/delete/113865/integrated
Del: useless -- https://master.php.net/note/delete/113865/useless
Del: bad code -- https://master.php.net/note/delete/113865/bad+code
Del: spam -- https://master.php.net/note/delete/113865/spam
Del: non-english -- https://master.php.net/note/delete/113865/non-english
Del: in docs -- https://master.php.net/note/delete/113865/in+docs
Del: other reasons-- https://master.php.net/note/delete/113865
Reject -- https://master.php.net/note/reject/113865
Search -- https://master.php.net/manage/user-notes.php