The ArrayAccess<Tk, Tv> interface ¶
(HHVM >= 2.5)
Introduction ¶
Interface to provide accessing objects as arrays.
Interface synopsis ¶
ArrayAccess<Tk, Tv> {
/* Methods */
}
Example #1 Basic usage
<?hhclass Obj<string, string> implements ArrayAccess<string, string> {
private array $container = array();
public function __construct() {
$this->container = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function offsetSet(string $key, string $value): this {
if (is_null($key)) {
$this->container[] = $value;
} else {
$this->container[$key] = $value;
}
}
public function offsetExists(string $offset): bool {
return isset($this->container[$offset]);
}
public function offsetUnset(string $offset): this {
unset($this->container[$offset]);
}
public function offsetGet(string $offset): ?string {
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 above example will output something similar to:
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 ) )
Table of Contents ¶
- ArrayAccess<Tk, Tv>::offsetExists — Whether a offset exists
- ArrayAccess<Tk, Tv>::offsetGet — Offset to retrieve
- ArrayAccess<Tk, Tv>::offsetSet — Offset to set
- ArrayAccess<Tk, Tv>::offsetUnset — Offset to unset
ArrayAccess<Tk, Tv>::offsetExists
(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetExists — Whether a offset exists
Description ¶
abstract public boolean ArrayAccess<Tk, Tv>::offsetExists ( Tk
$offset
)
Whether or not an offset exists.
Note:When using empty() ArrayAccess::offsetGet() will be called and checked if empty only ifArrayAccess::offsetExists() returnsTRUE
.
Parameters ¶
key
- An offset to check for.
Examples ¶
Example #1 ArrayAccess::offsetExists() example
<?hhclass Obj<Tk, Tv> implements ArrayAccess<Tk, Tv> {
public function offsetSet(Tk $offset, Tv $value): this {
var_dump(__METHOD__);
}
public function offsetExists(Tk $var): bool {
var_dump(__METHOD__);
if ($var == "foobar") {
return true;
}
return false;
}
public function offsetUnset(Tk $var): this {
var_dump(__METHOD__);
}
public function offsetGet(Tk $var): Tv {
var_dump(__METHOD__);
return "value";
}
}$obj = new Obj();
echo "Runs obj::offsetExists()\n";var_dump(isset($obj["foobar"]));
echo "\nRuns obj::offsetExists() and obj::offsetGet()\n";var_dump(empty($obj["foobar"]));
echo "\nRuns obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get\n";var_dump(empty($obj["foobaz"]));?>
The above example will output something similar to:
Runs obj::offsetExists() string(17) "obj::offsetExists" bool(true) Runs obj::offsetExists() and obj::offsetGet() string(17) "obj::offsetExists" string(14) "obj::offsetGet" bool(false) Runs obj::offsetExists(), *not* obj:offsetGet() as there is nothing to get string(17) "obj::offsetExists" bool(true)
ArrayAccess<Tk, Tv>::offsetGet
(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetGet — Offset to retrieve
Description ¶
Returns the value at specified offset.
This method is executed when checking if offset is empty().
Parameters ¶
key
- The offset to retrieve.
Notes ¶
Note:Starting with PHP 5.3.4, the prototype checks were relaxed and it's possible for implementations of this method to return by reference. This makes indirect modifications to the overloaded array dimensions of ArrayAccess<Tk, Tv>objects possible.A direct modification is one that replaces completely the value of the array dimension, as in $obj[6] = 7. An indirect modification, on the other hand, only changes part of the dimension, or attempts to assign the dimension by reference to another variable, as in $obj[6][7] = 7 or $var =& $obj[6]. Increments with ++ and decrements with -- are also implemented in a way that requires indirect modification.While direct modification triggers a call to ArrayAccess::offsetSet(), indirect modification triggers a call toArrayAccess::offsetGet(). In that case, the implementation of ArrayAccess::offsetGet() must be able to return by reference, otherwise anE_NOTICE
message is raised.
Return Values ¶
Can return all value types.
See Also ¶
- ArrayAccess::offsetExists() - Whether a offset exists
ArrayAccess<Tk, Tv>::offsetSet
(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetSet — Offset to set
Description ¶
abstract public this ArrayAccess<Tk, Tv>::offsetSet ( Tk
$key
, Tv $val
)
Assigns a value to the specified offset.
Parameters ¶
key
- The offset to assign the value to.
val
- The value to set.
Return Values ¶
this
Notes ¶
Note:Thekey
parameter will be set toNULL
if another value is not available, like in the following example.<?hh
$arrayaccess[] = "first value";$arrayaccess[] = "second value";print_r($arrayaccess);?>The above example will output:Array ( [0] => first value [1] => second value )
Note:This function is not called in assignments by reference and otherwise indirect changes to array dimensions overloaded with ArrayAccess (indirect in the sense they are made not by changing the dimension directly, but by changing a sub-dimension or sub-property or assigning the array dimension by reference to another variable). Instead,ArrayAccess::offsetGet() is called. The operation will only be successful if that method returns by reference, which is only possible since PHP 5.3.4.
ArrayAccess<Tk, Tv>::offsetUnset
(HHVM >= 2.5)
ArrayAccess<Tk, Tv>::offsetUnset — Offset to unset
Description ¶
abstract public this ArrayAccess<Tk, Tv>::offsetUnset ( Tk
$key
)
Unsets an offset.
Note:
Parameters ¶
key
- The offset to unset.
No comments:
Post a Comment