waArrayObject

Storage of values with array-like access

Contents...

This class allows creating a hierarchical storage of values with equal access to them either as items of multidimensional arrays or as objects containing nested collections of objects.

Examples

// the same: $a['b'] = 100;
$a->b = 100;

// the same: echo $a['b'];
echo $a->b;

// the same: $a['b']['c'] = 200;
$a->b->c = 200;

// the same: echo $a['b']['c'];
echo $a->b->c;

// properties containing other nested properties as instances of the waArrayObject class
$a->b->c = 200;
wa_dump($a->b); // waArrayObject object

Methods

  • __construct

    Class constructor.

  • setAll

    Saves specified values to object properties.

  • count

    Returns the number of 1st level properties.

  • toArray

    Returns object properties’ values as an associative array.

  • clear

    Deletes all object properties.

  • keyExists

    Verifies the availability of a 1st level object property with a specified name.

  • ifset

    Returns the value of a 1st level object property with a specified name or a default value.

  • ifempty

    Returns the value of a 1st level non-empty object property with a specified name or a default value.

public function __construct ($data = [])

Class constructor, which initializes object properties with specified values using the setAll() method.

Parameters

  • $data

    Value implementing the Traversable interface.

Example

$object = new waArrayObject([
    'part1' => 'Hello',
    'part2' => 'world!',
]);

echo $object->part1 . ' ' . $object->part2;

public function setAll ($data)

Saves items of a specified value implementing the Traversable interface to object properties.

Parameters

  • $data

    Value implementing the Traversable interface.

Example

$object = new waArrayObject();

$object->setAll([
    'part1' => 'Hello',
    'part2' => 'world!',
]);

echo $object->part1 . ' ' . $object->part2;

Result

Hello world!

public function count()

Returns the number of 1st level properties.

Example

$object = new waArrayObject([
    'foo' => 100,
    'bar' => [
        'test' => 200
    ],
]);

echo $object->count();

Result

2

public function toArray()

Returns object properties’ values as an associative array.

Example

$object = new waArrayObject([
    'foo' => 100,
    'bar' => [
        'qwerty' => 200
    ],
]);

wa_dump($object->toArray());

Result

[
    'foo' => 100,
    'bar' => [
      'qwerty' => 200,
    ],
]

public function clear()

Deletes all object properties.

Example

$object = new waArrayObject([
    'foo' => 100,
    'bar' => [
        'qwerty' => 200
    ],
]);

$object->clear();
wa_dump($object->toArray());

Result

[]

public function keyExists ($name)

Verifies the availability of a 1st level object property with a specified name.

Parameters

  • $name

    Name of a 1st level property.

Example

$object = new waArrayObject([
    'foo' => null,
    'bar' => false,
]);

wa_dump($object->keyExists('foo'));

Result

true

public function ifset ($name, $default = null)

Returns the value of a 1st level object property with a specified name or a default value.

Parameters

  • $name

    Name of a 1st level property.

  • $default

    Default value, which is returned if the 1st level object property whose name is specified in the $name parameter does not contain a value different from null.

Example

$object = new waArrayObject([
    'foo' => 100,
    'bar' => null,
]);

wa_dump($object->ifset('bar', 200));

Result

200

public function ifempty ($name, $default = null)

Returns the value of a 1st level non-empty object property with a specified name or a default value.

Parameters

  • $name

    Name of a 1st level property.

  • $default

    Default value, which is returned if the 1st level object property whose name is specified in the $name parameter does not contain a value that can be converted to Boolean true.

Example

$object = new waArrayObject([
    'foo' => 100,
    'bar' => 0,
]);

wa_dump($object->ifempty('bar', 200));

Result

200