[drupal-devel] Why the object notation?
Hi! Drupal code uses object notation very often and I fail to see why it's better than associative arrays. We get $_POST data in arrays so we need to convert back and forth. We never use the class construct. So why the -> ? It seems like the change can be done with at most two simple regexp replaces. Regards NK Ps. I think it's OK to initialize an array by $array['foo'] = 'bar'; this needs two steps with object notation. Only $array[] needs initialization.
On Thu, 28 Jul 2005, Karoly Negyesi wrote:
Drupal code uses object notation very often and I fail to see why it's better than associative arrays. We get $_POST data in arrays so we need to convert back and forth.
We never use the class construct. So why the -> ?
1) Dries prefers it 2) I prefer it too since -> is less typing than [''] (-> is two keystrokes for me, [''] is eight) Cheers, Gerhard
(-> is two keystrokes for me, [''] is eight)
Gerhard, it's true that -> is less keystrokes than [' '] but there is a problem -- since when do we treasure more developer time than running time?
I always thought that Drupal uses -> because it is less keystrokes, and it is this way since I am here [*], so at least since that time, developer time is prefered in this regard :) [*] Plus there was a move to convert even more stuff to objects from associative arrays, which mostly succeeded. Goba
On Thu, 28 Jul 2005, Karoly Negyesi wrote:
(-> is two keystrokes for me, [''] is eight)
Gerhard, it's true that -> is less keystrokes than [' '] but there is a problem -- since when do we treasure more developer time than running time?
Prove to me that execution time is greatly improved by using [''] and I will change my opinion. :o) Cheers, Gerhard
On Thu, 2005-07-28 at 07:58 +0200, Gerhard Killesreiter wrote:
On Thu, 28 Jul 2005, Karoly Negyesi wrote:
(-> is two keystrokes for me, [''] is eight)
Gerhard, it's true that -> is less keystrokes than [' '] but there is a problem -- since when do we treasure more developer time than running time?
Prove to me that execution time is greatly improved by using [''] and I will change my opinion. :o)
Object to array converisons take time. Otherwise there shouldn't be too much difference, since methods and member variables are stored in associative arrays. There is another case, apart from speed, though. Looping through hashes is immediate and more convenient than using objects. Vlado
I like what we have now: An array when it is a recurring 'thing'. An object when that thing is a single wel... 'object'. You don't want to iterated through all fields of a user, for they have all different kinds of functions. Hence it is an object. But a list of terms associated with a node comes as an array. Because you will most likely want to iterate over it and perform the same action to each entry. So, bottomline: A -1 on making it all one thing. Op donderdag 28 juli 2005 09:22, schreef vlado:
On Thu, 2005-07-28 at 07:58 +0200, Gerhard Killesreiter wrote:
On Thu, 28 Jul 2005, Karoly Negyesi wrote:
(-> is two keystrokes for me, [''] is eight)
Gerhard, it's true that -> is less keystrokes than [' '] but there is a problem -- since when do we treasure more developer time than running time?
Prove to me that execution time is greatly improved by using [''] and I will change my opinion. :o)
Object to array converisons take time.
Otherwise there shouldn't be too much difference, since methods and member variables are stored in associative arrays.
There is another case, apart from speed, though. Looping through hashes is immediate and more convenient than using objects.
Vlado Regards, Bèr -- [ Bèr Kessels | Drupal services www.webschuur.com ]
I like what we have now: An array when it is a recurring 'thing'. An object when that thing is a single wel... 'object'.
You don't want to iterated through all fields of a user, for they have all different kinds of functions. Hence it is an object. But a list of terms associated with a node comes as an array. Because you will most likely want to iterate over it and perform the same action to each entry. I'm fine with this logic. We should probably put an explicit explanation somewhere in the docs, for example in the coding style guidelines. I think there is a tendency to use objects vs arrays for estetic reasons only, which makes no real sense IMO.
Vlado
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 i far prefer array's vs objects since you can much more simply step through the elements. I also like the definition of arrays over objects (think of the menu arrays, and pretty soon the form arrays.) On 28 Jul 2005, at 2:41 AM, vlado wrote:
I like what we have now: An array when it is a recurring 'thing'. An object when that thing is a single wel... 'object'.
You don't want to iterated through all fields of a user, for they have all different kinds of functions. Hence it is an object. But a list of terms associated with a node comes as an array. Because you will most likely want to iterate over it and perform the same action to each entry.
I'm fine with this logic. We should probably put an explicit explanation somewhere in the docs, for example in the coding style guidelines. I think there is a tendency to use objects vs arrays for estetic reasons only, which makes no real sense IMO.
Vlado
- -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.4 (Darwin) iD8DBQFC6bdNgegMqdGlkasRAgt1AJ9lSmF4yvmZlwz4PCAEzLLVfc/ShACfXgw1 nl1H3NY/tc5Qo/hK2yZoVUg= =geEV -----END PGP SIGNATURE-----
I perfer the object notation because it (when using double quotes) makes string manipulations easier, and easier to read: "Title: <h2>$node->title</h2>" vs. 'Title: <h2>' . $node['title'] . '</h2>' On 7/28/05, Karoly Negyesi <karoly@negyesi.net> wrote:
Hi!
Drupal code uses object notation very often and I fail to see why it's better than associative arrays. We get $_POST data in arrays so we need to convert back and forth.
We never use the class construct. So why the -> ?
It seems like the change can be done with at most two simple regexp replaces.
Regards
NK
Ps. I think it's OK to initialize an array by $array['foo'] = 'bar'; this needs two steps with object notation. Only $array[] needs initialization.
participants (7)
-
Adrian Rossouw -
Bèr Kessels -
Earl Dunovant -
Gabor Hojtsy -
Gerhard Killesreiter -
Karoly Negyesi -
vlado