Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I find that even when working with a MVC framework (e.g. CodeIgniter), I regularly resort to passing nested arrays rather than objects.

This doesn't follow my understanding of object-oriented well. For example, if I were to port the code to Java then I wouldn't dream of passing arrays (or Vectors, or whatever) around like that.

I was wondering if this is the appropriate way to handle data. Is there a reason why arrays are passed around like this in PHP, or why objects aren't used? What's the best way to pass data around?

share|improve this question

4 Answers 4

Don't confuse Java's OO with PHP's. Java is a single paradigm language which means that it only does OO. PHP on the other hand is a multi paradigm language, you can either do functional programming or OO or both.

Now there is no such thing as a "bad" implementation of OO. Java's OO is not a definitive implementation that every other language should or must follow. There are some definitive concepts, and both languages fully implement them in their own way (Java since the beginning, PHP since version 5).

So, to answer your question: What CI does and what you are doing with it is right in the PHP world. PHP's arrays are one of its most flexible and useful structures and it's actually a good thing to use arrays over objects when your data are just information (do not carry logic with them). Fully OO code is not the same thing as "only OO code".

If you are starting with PHP do use Java as a reference for good OO practices but do not limit your understanding of PHP because of the "Java does it differently" thing. You can really screw up in both, the paradigm will not save you if you don't get the concepts.

If in any case you find Java more to your liking (as a personal preference) do check out Ruby for web development, they are a little bit more closely related.

share|improve this answer
1  
+1 re: Java OO definitive implementation –  sunwukung May 28 '11 at 12:49
    
Why should we not confuse Java OO with PHP OO? They are pretty much the same, except that PHP has a native array/hash datatype. –  Martin Wickman May 28 '11 at 13:06
    
The implementations are quite similar. I was referring to how they are used in both languages. –  Yannis Rizos May 28 '11 at 16:39

Usings objects instead of arrays just because it's objects is not OO paradigm, it's just personal preferences :)

Objects gives handy code completion in IDE, interfaces (type hinting) and inheritance can be used.

If you want to use objects instead of array because you see any benefits - use them, but if you want to use them just because it's objects - don't waste your time to this refactoring :)

share|improve this answer
    
"usage objects just as arrays will take more memory and CPU." This is not (always) true. An object holding the same amount of data with an array will occupy almost exactly the same amount of memory. –  Yannis Rizos May 27 '11 at 21:26
    
@Yannis Rizos, yes, objects even can use less memory, edited. –  OZ_ May 27 '11 at 21:37
    
It's not just that they can use less memory, it's that their memory use is irrelevant because it's trivial compared to other memory performance aspects of the system and to the benefits of using objects. If we cared about memory usage so much, we would never use objects. What we care about is that objects give us better tools to reason about and construct systems. –  Rein Henrichs May 27 '11 at 21:44
    
@Rein Henrichs, I know it, thanks. That's why all words about 'memory' was removed from my answer. I'm not trying to do silly optimization, but I'm sure objects just because they are objects is not a reason for refactoring :) Read my answer, not only comments. –  OZ_ May 27 '11 at 21:49
    
fair enough, I commented before the edits. –  Rein Henrichs May 27 '11 at 21:50

You don't really pass data around in an OO system -- you pass objects around. The difference is that objects contains behavior as well as data. That is why they call it object oriented and not data oriented.

As long as your don't need behavior with your data, then plain old php arrays are just as good (or bad, depending on your point of view) as value objects.

share|improve this answer

I think this is just a question of adjustment - there are many implementations of "objects" in programming - Python and Javascript have strikingly different properties. PHP OO is arguably a hack - PHP arrays aren't "objects" in the traditional sense - but they serve an obvious purpose. Unless you want the data to have custom BEHAVIOUR then why use an object?

EDIT:

re: immutable value objects

http://bradley-holt.com/2010/09/immutable-value-objects-in-php/

share|improve this answer
    
To give the data a reliable structure. Yep, I can build a function that outputs a particular format/nesting of array but who's to say I don't write another function that modifies it slightly and then passes it on. –  Matt Potts May 28 '11 at 0:03
1  
sure - then you would need an object to protect variable access - since there are no PHP equivalents of Python tuples for example. But there you go - you've defined the problem, you've just got to use the constructs available to the language... or just learn to live with it's "quirks". I've added a link to my initial post that has an interesting discussion of the problem. –  sunwukung May 28 '11 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.