|
Previous Class |
Solar_Struct |
Next Page |
Solar_Struct
Greatly simplified one-dimensional array object.
Using this class, you can access data using both array notation ($foo['bar']) and object notation ($foo->bar). This helps with moving data among form objects, view helpers, SQL objects, etc.
Examples ...
<?php
$data = array('foo' => 'bar', 'baz' => 'dib', 'zim' => 'gir');
$struct = Solar::factory('Solar_Struct', array('data' => $data));
echo $struct['foo']; // 'bar'
echo $struct->foo; // 'bar'
echo count($struct); // 3
foreach ($struct as $key => $val) {
echo "$key=$val ";
} // foo=bar baz=dib zim=gir
$struct->zim = 'irk';
echo $struct['zim']; // 'irk'
$struct->noSuchKey = 'nothing';
echo $struct->noSuchKey; // null
?>
One problem is that casting the object to an array will not reveal the data; you'll get an empty array. Instead, you should use the toArray() method to get a copy of the object data.
<?php
$data = array('foo' => 'bar', 'baz' => 'dib', 'zim' => 'gir');
$object = Solar::factory('Solar_Struct', array('data' => $data));
$struct = (array) $object; // $struct = array();
$struct = $object->toArray(); // $struct = array('foo' => 'bar', ...)
?>
Another problem is that you can't use object notation inside double- quotes directly; you need to wrap in braces.
<?php
echo "$struct->foo"; // won't work
echo "{$struct->foo}"; // will work
?>
A third problem is that you can't address keys inside a foreach() loop directly using array notation; you have to use object notation. Originally reported by Antti Holvikari.
<?php
// will not work
foreach ($struct['subarray'] as $key => $val) { ... }
// will work
foreach ($struct->subarray as $key => $val) { ... }
?>
Catalog
This class is part of the Solar package.
Inheritance:
- Solar_Base
- Solar_Struct
Constants
None.
Public Properties
The Solar_Struct class has no public properties; try the list of all properties.
Public Methods
These are all the public methods in the Solar_Struct class.
You can also view the list of all public, protected, and private methods.
__construct()- Constructor.
__destruct()- Default destructor; does nothing other than provide a safe fallback for calls to parent::__destruct().
__get()- Gets a data value.
__isset()- Does a certain key exist in the data?
__set()- Sets a key value.
__unset()- Sets a key in the data to null.
apiVersion()- Reports the API version for this class.
count()- Countable: how many keys are there?
current()- Iterator: get the current value for the array pointer.
dump()- Convenience method for getting a dump the whole object, or one of its properties, or an external variable.
key()- Iterator: get the current key for the array pointer.
load()- Loads the struct with data from an array or another struct.
locale()- Looks up class-specific locale strings based on a key.
next()- Iterator: move to the next position.
offsetExists()- ArrayAccess: does the requested key exist?
offsetGet()- ArrayAccess: get a key value.
offsetSet()- ArrayAccess: set a key value.
offsetUnset()- ArrayAccess: unset a key.
rewind()- Iterator: move to the first position.
toArray()- Returns a copy of the object data as an array.
valid()- Iterator: is the current position valid?