Zend Framework: Why not ArrayAccess?

Updated:

I’m pretty much kicking myself. In all my tests, I never once thought about returning an actual array!

“Pfft, that would be too easy!”

I suck.

Lines 35, 36: Add ‘ArrayAccess’ to the end of the line 35

class Zend_Config implements Countable, Iterator, ArrayAccess
{

Add anywhere in the class that you feel makes sense. In my version 0.1.5 preview release, I placed it in lines 80-98:

public function offsetExists($key)
{
	return $this->__isset($key);
}

public function offsetGet($key)
{
	return $this->__get($key);
}

public function offsetSet($key, $value)
{
	return $this->__set($key, $value);
}

public function offsetUnset($key)
{
	return $this->__set($key, null);
}

Yeah, I mean. After looking at the code a little bit, it became somewhat clearer on how the object functioned and how it got passed my previous post restrictions. Really simple actually.

The point I should make is that offsetSet bypasses the protected magic Set method, therefore, since I can’t test it on 5.0, it would be hard to say if it would still work or not.

The issue of code duplication is resolved by using methods that already exist. For objects that return arrays, it is possible to use the same methods listed above for any class that also use the property overloading magic methods.

Use at your own risk!

Uses the Zend Framework Config class.

$DoNotTouch = new Zend_Config(Zend_Config_Xml::load('/path/to/config.xml', 'secretdata'));

// Property

$value = $DoNotTouch->key->key;

// Or Array

$secret = $DoNotTouch->asArray();    // Iterates over all values and returns as PHP Array
$value = $secret['key']['key'];

Or simply and without as much overhead:

$DoNotTouch = new Zend_Config(Zend_Config_Xml::load('/path/to/config.xml', 'secretdata'));

// Property

$value = $DoNotTouch->key->key;

// Or ArrayAccess
$value = $DoNotTouch['key']['key'];

Possibly Related Posts:


5 Comments.

  1. I remember a brief set of posts on the ZF General mailing list about creating a standard class for the type of data store Zend_Config and a few other components use. There is a bit of duplication afterall in accessing values through object chaining.

    You should seek it out – it may have mentioned ArrayAccess. And if implemented, and if it implements ArrayAccess, and if Zend_Config uses it by 0.7 (next release after 0.2) then you will have what you want ;) .

    A lot of “if”s in there – I only vaguely recall the email…

  2. I did check that out during the various revisions. Nope, it seems that Property overloading is going to be “the thing” for Zend Framework. Not that it is a bad thing. It would just take too much refactoring.

    For instance, you would have to create a Zend_Array_Xml for SimpleXML, and perhaps DOM and XMLReader. Not cool. You wouldn’t create a Zend_Array_Generic, because it already exists with ArrayObject.

    I was thinking about it, but as many smart, intelligent, and talented people that are working on the Zend Framework, anything I would submit in a Proposal would have to be labeled as “crap”.

    There’s nothing on Zend_Hash or whatever, on the Zend Framework. I didn’t look that far, but I really don’t want to create something that will exist by the time I’m finished.

  3. We all are sometimes…;). I’ll be looking it up myself too. I’m using the ZF and watching the mailing list is a big benefit. I’m waiting for 0.2, because that will see a lot of API changes.

    FYI, proposals and questions to the mailing list are received quite well. The overall list attitude is quite positive and I’ve not yet seen any signs of impatience or elitism – all round quite a friendly forum.

  4. Yeah, I was thinking about doing that from now on. It would be interesting to talk to the guy who wrote it.

    Anyway, I’ll probably email you about something.