PHP Array vs Object Array

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

PHP Array vs Object Array

Post by santosj »

I was thinking about what a blogger said about why someone hasn't already made an Array Object for PHP since the 'array functions names are not uniformed.' However, my first response was that anyone who is a PHP coder and using arrays can pick up the built-in functions and once you know the function and hone the skill and love for the function then there is little anyone can do to move you away. Until you read something or find code that says otherwise.

Also, the PHP functions are with almost every PHP version, so you don't need to include a class and make sure that all of the developers know how to use it. ArrayObject still blows my mind in its implementation and its use.

The thought did stay in the back of my mind, chewing and scratching its way forward (much like the bastard that suggested I shave 'that' area ending in the same results).

The issues

The speed of the Array object over the native array and functions.
The container for the built-in functions as class methods, do you change the name or do you alias and add support for the other?

Possible in PHP 5

While I have created code that looked like arrays, I have the issue of whether or not you return the scalar or array. I did think of having a stop key to determine whether or not to return the value or to return the arrayobject.

Code: Select all

<?php
$array['test'] = 'one'; // Hides help, continue, and parity
$array['test']['help'] = 'me';
$array['test']['continue'] = 'maybe';
$array['test']['continue']['maybe'] = 'okay do it'; // Fails
$array['test']['parity'] = '0';
$array['script'][] = 0;
$array['script'][] = 1;
$array['script'][] = 2;

var_dump($array);
?>
So I think I could implement something that performs similar to the way PHP native array performs. I thought Arrays acted differently, so I was surpised when it was also limited as is SPL ArrayAccess.

The Question

Would this be a good idea? I don't think so, but I'm still going to implement it so that I can get it off my mind.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Personally, even though the idea of array=object found in many other languages can be appealing, I think trying to implement it at this level would be extremely inefficient.

That being said, you're correct, it is possible in PHP5. However, that functionality is usually used to give objects an array-like interface.

No harm in trying though. :-)
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

Well, what I liked about D, not to go off topic, but that some of the most used methods are built-in to the native array type. However the D syntax

Code: Select all

array.length
compared to either

Code: Select all

count($array);
Or

Code: Select all

$array->length();
I would rather to use the D one.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Agreed. (you see the same behavior in JavaScript too)
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

I'm getting intimate with JavaScript Classes (which I think is a lot like Perl 5 classes) and the dot, I think is somewhat prettier than '->'.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

The ruby syntax is similar to D.

Code: Select all

ary = [1,2,3]
ary.join(":")		(gives 1:2:3)
ary.split(":")		(recreates original array)
ary.length		(length of 3)
It's quite easy to work with - better than with PHP IMO.
Post Reply