Php-ArrayAccess interface

Source: Internet
Author: User
The pre-defined PHP interface-ArrayAccess interface provides interfaces that can access objects just like an array.

Interface abstract

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

Example #1 Example

 container = array(                "one" => 1,                "two" => 2,                "three" => 3,            );        }        public function offsetSet($offset, $value) {            if (is_null($offset)) {                $this->container[] = $value;            } else {                $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"]);    $obj[] = 'Append 1';    $obj[] = 'Append 2';    $obj[] = 'Append 3';    print_r($obj);?>

The output of the preceding routine is similar:

bool(true)int(2)bool(false)string(7) "A value"obj Object(    [container:obj:private] => Array        (            [one] => 1            [three] => 3            [two] => A value            [0] => Append 1            [1] => Append 2            [2] => Append 3        ))

Method list

ArrayAccess: offsetExists-check whether an offset exists.

ArrayAccess: offsetGet-obtains the value of an offset.

ArrayAccess: offsetSet-set the value of an offset.

ArrayAccess: offsetUnset-reset the value of an offset.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.