OOP vs. Procedural *Singleton* (edit)

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

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Aha, but a class implementing ArrayAccess is not a native Array per se
Aha, but a Registry is not a ServiceLocator per se :lol:

Jokes aside, it seems class-based Registry does not provide anything the array can't (including future extensions possibility).
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

A class based Registry offers flexibility... I'm not disagreeing really, but an array is the simplest form of a Registry's behaviour - the simplest form is not always the required future behaviour. I'd always start with the class first under the assumption it would grow in complexity in the future.

I forgot to note Type Hinting as a possible reason to use a class.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

So, why use more complex class-based approach?
By not using an object you lose the benefits of a defined interface, encapsulation and information hiding ... to name just three reasons. Those are pretty important to large scale development with hundreds of developers. I wonder if you can get away with that on the small and medium scale though? Where the lines of communications are simpler and the supervision is tighter.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

By not using an object you lose the benefits of a defined interface...
Arrays don't have a defined interface? Now that's interesting :)
...encapsulation...
by implementing ArrayAccess in the future you would get the encapsulation (and 'enhanced behaviour') you want without sacrificing interface simplicity. And to start with you do not create excessive entities just to wrap (or 'glorify', in Maugrim's terms) plain old arrays. Occam's razor in action. I heard agile developers invented a new term for this 600+ years old concept: YAGNI.
information hiding ...
see above.

What I argue is not that class-based Registry in some ways inferior to array. It's that the array interface is most logical to tasks the Registry performs.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Interesting arguments, Weirdan, maybe one has to be vigilant for more syntactic sugar wrappings like these.

In my view simplicity indeed pays off - and as far as interface simplicity the array is much more intuitive than an object. A parallel in C++ is the concept of smart pointers - they are behaving just like regular pointers, using the usual native syntax (an example of the same thing gone wrong is the cin and cout-style predefinitions of shift operators - they don't behave like the usual shift)

A possible drawback for some (like me) is that in PHP4 the arrays are "just arrays", but for the majority of cases this would be enough.
Post Reply