Overview

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->addNewKey = 'something new has been added';
echo $struct->noSuchKey; // 'something new has been added'

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) { ... }

Package

This class is part of the Solar package.

Inheritance:

Configuration Keys

  • data: Key-value pairs.

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 and marks the struct as "dirty".

__toString()

Returns a string representation of the object.

__unset()

Sets a key in the data to null.

count()

Countable: how many keys are there?

dump()

Convenience method for getting a dump the whole object, or one of its properties, or an external variable.

free()

Frees memory used by this struct.

getIterator()

IteratorAggregate: returns an external iterator for this struct.

getKeys()

Returns all the keys for this struct.

isDirty()

Is the struct dirty?

load()

Loads the struct with data from an array or another struct.

locale()

Looks up class-specific locale strings based on a key.

offsetExists()

ArrayAccess: does the requested key exist?

offsetGet()

ArrayAccess: get a key value.

offsetSet()

ArrayAccess: set a key value.

offsetUnset()

ArrayAccess: unset a key.

toArray()

Returns a copy of the struct as an array, recursively descending to convert child structs into arrays as well.

toString()

Returns a string representation of the struct.



Local