My Love For Array Object Idiocy

I suppose I’m fixated on objects, but damn it, they are awesome. Recently, I’ve been thinking of creating a framework that takes array and file functions and creates a class wrapper around them. It would have been nice if PHP offered an object for handling Arrays and Files. SPL does manage some sorting for arrays, but I would like to fully manage arrays through the internal Array methods.


$data = new Array('key1' => 'value1', 'key2' => 'value2');
$data['key3'] = 'value3';
$data[] = 'value4';

// Pushing data => array_push()
$data->push('value5', 'value6');

// Shifting Data => array_shift()
$value1 = $data->shift();

Some of this is possible in the userland. The constructor part is not, but it is possible to create the array using the array function and using that for the constructor.


$array = array('key1' => 'value1', 'key2' => 'value2');
$data = new Array($array);

Using SPL ArrayObject

The SPL ArrayObject allows for this functionality, but the central issue is the speed decrease that would be introduced on something that would be rarely used. Why spend days or weeks creating something that even I wouldn’t use?


class Array extends ArrayObject
{
    protected $data = array();

    // Other functions
}

Appending Arrays

The interesting part however is the automatic appending that the objects would offer. Oh yes, days of C++ coding do not fail me or the parts that I still remember. I have a C++ book, which stores all of my knowledge for me, such as the PHP.net stores all of my PHP language and function knowledge.

Oh yes, I spent many of hours building a class in C++ for appending classes and doing almost the exact same thing that I want to do for PHP. Hey, if it half assed worked for C++, then it should work for PHP, except I would have to pull out the C++ code to write it as an extension, so it wouldn’t reduce the speed of the script all that much.

Why Not Userland

I may write a test class just to see how much overhead the object brings. I suppose the only complexity the object would bring is that you can’t have ArrayAccess access multiple levels.

$data['level1']['level2'] = 'whatever';

Won’t work, unless you reference level1 to another ArrayAccess object. So, you wouldn’t have just the Array object, you would need another object, like maybe ArrayArray (ha ha, ah damn), for when you need multi-dimensional arrays.

The normal array usage is that you can’t have a multi-dimensional key along with a normal key.


$array['key1'] = 'value';
$array['key1']['key2'] = 'value2';

Does not return any errors, but will return

Array
(
    [key1] => value
)

Which isn’t the expected or wanted result. If you try to access $array['key1']['key2'], you won’t get ‘value2′.

My Tests

I put a lot of time and testing into trying to allow for the above example, but it is impossible. It is impossible to do because you have no idea what level the user stops at. You could create some hackish way around it.

My thoughts on a possible fix was to advance the level when ever the object is returned and process whether or not they stopped in some fashion. It is still a work in progress and I haven’t devoted any time to testing the level theory.

If the normal array usage follows that of ArrayAccess, then I really don’t a reason of trying to ‘correct’ it. However, I’m pretty sure that it should still be possible. My only conclusion is that $array['key1'] is a pointer and can only point to a scalar or another array and not both. I would like it if ArrayAccess return what level the user wants to access.

Possibly Related Posts:


1 Comments.

Trackbacks and Pingbacks: