The ArrayObject class

(PHP 5, PHP 7, PHP 8)

Вступ

This class allows objects to work as arrays.

Зауваження: Wrapping objects with this class is fundamentally flawed, and therefore its usage with objects is discouraged.

Короткий огляд класу

class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable {
/* Константи */
public const int STD_PROP_LIST;
public const int ARRAY_AS_PROPS;
/* Методи */
public __construct(array|object $array = [], int $flags = 0, string $iteratorClass = ArrayIterator::class)
public append(mixed $value): void
public asort(int $flags = SORT_REGULAR): true
public count(): int
public getFlags(): int
public ksort(int $flags = SORT_REGULAR): true
public natcasesort(): true
public natsort(): true
public offsetExists(mixed $key): bool
public offsetGet(mixed $key): mixed
public offsetSet(mixed $key, mixed $value): void
public offsetUnset(mixed $key): void
public serialize(): string
public setFlags(int $flags): void
public setIteratorClass(string $iteratorClass): void
public uasort(callable $callback): true
public uksort(callable $callback): true
public unserialize(string $data): void
}

Попередньо визначені константи

ArrayObject Flags

ArrayObject::STD_PROP_LIST

Properties of the object have their normal functionality when accessed as list (var_dump(), foreach, etc.).

ArrayObject::ARRAY_AS_PROPS

Entries can be accessed as properties (read and write). The ArrayObject class uses its own logic to access properties, thus no warning or error is raised when trying to read or write dynamic properties.

Зміст

add a note

User Contributed Notes 10 notes

up
98
php5 dot man at lightning dot hu
12 years ago
As you know ArrayObject is not an array so you can't use the built in array functions. Here's a trick around that:

Extend the ArrayObject class with your own and implement this magic method:

<?php
public function __call($func, $argv)
{
if (!
is_callable($func) || substr($func, 0, 6) !== 'array_')
{
throw new
BadMethodCallException(__CLASS__.'->'.$func);
}
return
call_user_func_array($func, array_merge(array($this->getArrayCopy()), $argv));
}
?>

Now you can do this with any array_* function:
<?php
$yourObject
->array_keys();
?>
- Don't forget to ommit the first parameter - it's automatic!

Note: You might want to write your own functions if you're working with large sets of data.
up
33
rwn dot gallego at gmail dot com
11 years ago
There is a better explanation about the ArrayObject flags (STD_PROP_LIST and ARRAY_AS_PROPS) right here:

http://stackoverflow.com/a/16619183/1019305

Thanks to JayTaph
up
12
bub at gmail dot com
2 years ago
If you need the last key of your collection use:
<?php
array_key_last
($this->getArrayCopy())
?>

In an extending class it could look like:
<?php
class Collection extends ArrayObject
{
public function
lastKey(): int
{
return
array_key_last($this->getArrayCopy());
}
}
?>

If you want to use any type safe collection:
<?php
class BookCollection extends Collection
{
public function
add(Book $book) : void
{
$this->offsetSet($book->id, $book);
}

// note the return type "Book"
public function get(int $bookId) : Book
{
$this->offsetGet($bookId);
}
}
?>
up
4
hello at rayfung dot hk
3 years ago
If you want to use built-in array function with ArrayObject, store the iterator instance and return the value as reference in offsetGet.

<?php
class Collection extends \ArrayObject {
public function
__construct(array $data = [])
{
if (!
\is_array($data) && !\array_key_exists('ArrayAccess', class_implements($data))) {
$data = [$data];
}

$this->iterator = $this->getIterator();
parent::__construct($data);
}

public function &
offsetGet($index)
{
$value = &$this->iterator[$index] ?? null;

return
$value;
}
}
?>
up
24
MarkAndrewSlade at gmail dot com
13 years ago
I found the description of STD_PROP_LIST a bit vague, so I put together a simple demonstration to show its behavior:

<?php

$a
= new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
$a['arr'] = 'array data';
$a->prop = 'prop data';
$b = new ArrayObject();
$b['arr'] = 'array data';
$b->prop = 'prop data';

// ArrayObject Object
// (
// [prop] => prop data
// )
print_r($a);

// ArrayObject Object
// (
// [arr] => array data
// )
print_r($b);

?>
up
10
deminy at deminy dot net
15 years ago
Generally variable $this can't be used as an array within an object context. For example, following code piece would cause a fatal error:

<?php
class TestThis {
public function
__set($name, $val) {
$this[$name] = $val;
}

public function
__get($name) {
return
$this[$name];
}
}

$obj = new TestThis();
$obj->a = 'aaa';
echo
$obj->a . "\n";
?>

But things are different when $this is used in an ArrayObject object. e.g., following code piece are valid:

<?php
class TestArrayObject extends ArrayObject {
public function
__set($name, $val) {
$this[$name] = $val;
}

public function
__get($name) {
return
$this[$name];
}
}

$obj = new TestArrayObject();
$obj->a = 'aaa';
echo
$obj->a . "\n";
?>
up
9
Gilles A
10 years ago
// Example STD_PROP_LIST and ARRAY_AS_PROP combined
<?php
$ao
= new ArrayObject();
$ao ->setFlags(ArrayObject::STD_PROP_LIST|ArrayObject::ARRAY_AS_PROPS);

$ao->prop = 'prop data';
$ao['arr'] = 'array data';

print_r($ao);

?>

// Result

ArrayObject Object
(
[storage:ArrayObject:private] =&gt; Array
(
[prop] => prop data
[arr] => array data
)

)
up
3
Vuong Nguyen
6 years ago
You can easily realise that ArrayObject can use various functions as they are in ArrayIterator to iterate an object-as-a-array. However, you need to "activate" these function (rewind, valid, next and so on...) by using getIterator() first. Actually this function inherits from Iterator Aggregate interface.

Take a look at the following basic example. The results are the same:

<?php

$array
= [1, 2, 3, 4];
$a = new ArrayObject($array);
$b = new ArrayIterator($array);

$iterator = $a->getIterator();

for(
$iterator->rewind(); $iterator->valid(); $iterator->next()){
echo
$iterator->current()*2;

}

for(
$b->rewind(); $b->valid(); $b->next()){
echo
$b->current()*2;

}

//Resulst are the same 2468 AND 2468
up
6
sfinktah at php dot spamtrak dot org
13 years ago
If you plan to derive your own class from ArrayObject, and wish to maintain complete ArrayObject functionality (such as being able to cast to an array), it is necessary to use ArrayObject's own private property "storage".

Since that is impossible to do directly, you must use ArrayObject's offset{Set,Get,Exists,Unset} methods to manipulate it indirectly.

As a side benefit, this means you inherit all the iteration and other functions in complete working order.

This may sound obvious to someone who has never implemented their own ArrayObject class... but it is far from so.

<?php

class MyArrayObject extends ArrayObject {
static
$debugLevel = 2;

static public function
sdprintf() {
if (static::
$debugLevel > 1) {
call_user_func_array("printf", func_get_args());
}
}

public function
offsetGet($name) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",", func_get_args()));
return
call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
public function
offsetSet($name, $value) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",", func_get_args()));
return
call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
public function
offsetExists($name) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",", func_get_args()));
return
call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
public function
offsetUnset($name) {
self::sdprintf("%s(%s)\n", __FUNCTION__, implode(",", func_get_args()));
return
call_user_func_array(array(parent, __FUNCTION__), func_get_args());
}
}

$mao = new MyArrayObject();
$mao["name"] = "bob";
$mao["friend"] = "jane";
print_r((array)$mao);

/* Output:

offsetSet(name,bob)
offsetSet(friend,jane)
Array
(
[name] => bob
[friend] => jane
) */
?>

If you wish to use the "Array as Properties" flag, you simply need to include this in your constructor:

<?php parent::setFlags(parent::ARRAY_AS_PROPS); ?>

This will allow you to do things such as the below example, without overriding __get or __set .

<?php
$mao
->name = "Phil";
echo
$mao["name"]; /* Outputs "Phil" */
?>
up
3
rudie
6 years ago
If you want numerical ArrayObject objects to play nice with json_encode(), implement JsonSerializable:

class JsonSerializableArrayObject extends ArrayObject implements JsonSerializable {
function jsonSerialize() {
return $this->getArrayCopy();
}
}

For assoc ArrayObject objects this isn't neccesary, but for numerical arrays it is, otherwise they will be formatted like

{"0":"jaap","1":"karel"}

instead of

["jaap","karel"]
To Top