Predefined Interfaces
PHP Manual

The ArrayAccess interface

Ââåäåíèå

Interface to provide accessing objects as arrays.

Class synopsis

ArrayAccess
ArrayAccess {
/* Methods */
abstract public boolean ArrayAccess::offsetExists ( string $offset )
abstract public mixed ArrayAccess::offsetGet ( string $offset )
abstract public void ArrayAccess::offsetSet ( string $offset , string $value )
abstract public void ArrayAccess::offsetUnset ( string $offset )
}

Ïðèìåð #1 Basic usage

<?php
class obj implements arrayaccess {
    private 
$container = array();
    public function 
__construct() {
        
$this->container = array(
            
"one"   => 1,
            
"two"   => 2,
            
"three" => 3,
        );
    }
    public function 
offsetSet($offset$value) {
        
$this->container[$offset] = $value;
    }
    public function 
offsetExists($offset) {
        return isset(
$this->container[$offset]);
    }
    public function 
offsetUnset($offset) {
        unset(
$this->container[$offset]);
    }
    public function 
offsetGet($offset) {
        return isset(
$this->container[$offset]) ? $this->container[$offset] : null;
    }
}

$obj = new obj;

var_dump(isset($obj["two"]));
var_dump($obj["two"]);
unset(
$obj["two"]);
var_dump(isset($obj["two"]));
$obj["two"] = "A value";
var_dump($obj["two"]);

?>

Ðåçóëüòàòîì âûïîëíåíèÿ äàííîãî ïðèìåðà áóäåò ÷òî-òî ïîäîáíîå:

bool(true)
int(2)
bool(false)
string(7) "A value"

Ñîäåðæàíèå


Predefined Interfaces
PHP Manual

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/toplevels/data/www/maripoza.ru/fc7a82cd8e0116192ce432b06b9bd9c9/sape.php on line 219

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/toplevels/data/www/maripoza.ru/fc7a82cd8e0116192ce432b06b9bd9c9/sape.php on line 225