AnewtContainer class

Generic container class to hold data.

Class Overview

Generic container class to hold data.

This class is a data container and provides a number of methods to get, set, add, import and export data. See the core module documentation for more information on how to use this class efficiently.

Public Methods

__construct($data=null)

Create a new AnewtContainer instance.

An optional parameter can be supplied to fill the container with initial values. This is the same as calling seed() manually. You may safely override the constructor for your own classes; there is no need to call the parent constructor.

Parameters

$data

An associative array with the initial data (optional)

See also

Methods for handling data

These methods can be used to get and set data. These methods may call into special getter and setter methods. Most methods have a corresponding non-magic method that does not invoke special getter and setter methods.

get($name)

Return the data referenced by name.

By default just returns the data referenced by $name. If you write a class that extends AnewtContainer, you can write special methods to override the behaviour of the AnewtContainer::get() function. In order to do this, you can write a get_foo() method which will be invoked if you use get('foo'). This allows for a consistent API, optional getter and setter methods and on-the-fly generation of data.

This method supports basic caching of values. If you write a get_foo_() method (note the trailing underscore!) and you call $obj->get('foo'), the value will be cached and your special method will only be called once. Note that regular getters, eg. get_foo() always have precedence over the caching methods. You should provide either get_foo() or get_foo_(), not both. This type of caching is very useful if you do database queries or other computationally intensive things in your getter method and you only need to do it once per instance.

Parameters

$name

The name of the value to get

Return value

The requested data.

See also

__get($name)

Alias function for get.

Parameters

$name

See also

getdefault($name, $default=null)

Return the data by name, or a default value.

Parameters

$name

The name to get.

$default

The default value, in case no value was set

See also

set($name, $value)

Store a value in the container.

You can write special methods like set_foo() accepting a variable number of parameters to override the default behaviour of this methods. See the documentation for AnewtContainer::get() for more information.

Parameters

$name

The name of the data to store (int or string).

$value

The value of the data to store.

See also

__set($name, $value)

Alias function for set.

Parameters

$name

$value

See also

setdefault($name, $default)

Set a default value if no value was previously set.

This method checks if the variable referenced by $name is set. If not, it sets the default value. If the variable is already set, this method does nothing at all.

Parameters

$name

The name to set

$default

The default value to store, in case no value was set before

See also

add($name, $value)

Add data to a list.

You can write special methods like add_foo() accepting a variable number of parameters in classes extending AnewtContainer. See the documentation for AnewtContainer::get() for more information.

Parameters

$name

The name of the data to store (int or string).

$value

The value of the data to add.

See also

delete($name=null)

Delete a value from the container.

This method is only useful for simple data fields (i.e. without custom getter/setter methods) and for those with cached get methods. In the last case, unsetting the saved data will make sure the data is 'invalidated', so the get-method gets re-executed the next time the value is needed.

Parameters

$name

The name of the data to unset (int or string).

See also

__unset($name)

Alias function for delete.

Parameters

$name

See also

is_set($name)

Check whether a value for the name specified exists.

This methods checks whether a value exists for the specified name, or whether a special getter function for it exists. Use this method if you want to be sure you can call get($name) without errors later on.

Parameters

$name

The name of the data to check for (int or string).

Return value

True if the data is available, false otherwise.

See also

__isset($name)

Alias function for is_set.

Parameters

$name

See also

seed($data, $keys=null)

Seed the container with data.

This method lets you populate the container instance with data from an associative array, e.g. a database result or a hardcoded array of initial values.

Parameters

$data

An array or object containing data. Both numeric, associative and mixed arrays are supported, as well as AnewtContainer objects (or anything that provides a valid to_array() method.

$keys

Optional parameter that is used to selectively seed this container with data. If used in combination with an array of $data, this parameter is treated as a list of keys that should be used. If used in combination with an $data object, the parameter is passed to $obj->to_array() unmodified.

See also

clear()

Remove all data from the container.

This removes all values from the internal storage. Magic getter methods may still yield results afterwards, but the internal storage will be empty. Usually you don't need this method: use a new instance if you want to work with a different set of data.

See also

keys()

Return a list of all defined names.

This method includes both keys in the internal storage and the name of values available only through getter methods.

Return value

A numeric array with all keys.

See also

to_array($keys=null)

Export all available data to an associative array.

This method tries its best to get all the available data. It returns all the variables stored in the internal __data storage and calls all get_* methods too (this is off by default). Advanced getter methods that take parameters should provide a default value, otherwise the conversion is not going to work. Note that if both internal storage and getters are available for the same data, the latter has precedence, eg. the internal storage is ignored.

Parameters

$keys

A numeric array or a boolean value. If a numeric array is supplied it will be used as a list of keys (passed to the get() method of the $container). If boolean false is supplied, only the internal data of the container is returned and special getter methods are not invoked. If a boolean true is supplied, all data including the data returned by special getter methods on the container is returned. Note that using boolean true might result in a very expensive operation (all getter methods are invoked!), so it should be used with care. If omitted (or null), this parameter defaults to boolean false (only internal container storage is returned).

Return value

An associative array containing all the data from this container.

See also

Non-magic methods for handling data

These methods have roughly the same API as their magic counterparts, but don't call special getter and setter methods, so they work for simple values only. This is considerably faster if you're e.g. looping over many AnewtContainer instances and extracting some values.

_get($name)

Return the data referenced by name (non-magic).

Parameters

$name

The name to get.

Return value

Associated value.

See also

_getdefault($name, $default=null)

Return the data by name, or a default value (non-magic).

Parameters

$name

The name to get.

$default

The default value to return if no value for $name was stored before.

Return value

The associated value or the default value supplied.

See also

_set($name, $value)

Store a value in the container (non-magic).

Parameters

$name

The name of the data to store.

$value

The value of the data to store.

See also

_add($name, $value)

Add data to a list (non-magic).

Parameters

$name

The name of the list to add to.

$value

The value of the data to add.

See also

_isset($name)

Check whether a value for the name specified exists (non-magic).

Parameters

$name

Name to check for.

Return value

True if set, false otherwise.

See also

_seed($data)

Populates the internal array from an array (non-magic).

Parameters

$data

An associative array with data.

_keys()

Return a list of all defined names (non-magic).

Return value

A numeric array with all internal keys.

See also

_to_array()

Export all name-value pairs to an associative array (non-magic).

This method does not call into getter methods.

Return value

Array (copy) containing the internal storage.

See also

Private Attributes

$__data [private]

Internal storage.

This array holds all the values stored in the container.