Page 1 of 1
Classes vs. Function Libraries
Posted: Sat Oct 10, 2009 9:21 am
by volomike
On Reddit, someone mentioned something interesting. He said that since PHP does not persist objects (at least yet) between pages and sessions, then why do we use them? He asked why we don't just make PHP frameworks that are 100% function based? Sure, he was a fan of objects on like the Java platform where objects can be persisted efficiently between pages and sessions, but in the case of PHP, he didn't see the point of that.
For example, look at the differences here:
OOP
$oRec = new Record('apple');
$oRec->Color = 'red';
$oRec->Type = 'Delicious';
$oRec->saveRecord();
Functions
$asRec['Color'] = 'red';
$asRec['Type'] = 'Delicious';
Records_saveRecord($asRec);
He postulated that functions would run faster than the OOP. True? Not True?
Not that I entirely believe this line of reasoning, but I wanted to put it out to hear your thoughts.
Re: Classes vs. Function Libraries
Posted: Sat Oct 10, 2009 11:50 am
by JNettles
I'm confused as to what the actual proposal is in terms of improvement to PHP - web applications simply don't behave in this way.
When it comes right down to it, web servers behave in a very linear way - a browser requests a specific page from the server along with some parameters for how it would like the page to run and be displayed, but the web server simply crunches the code and returns the HTML, no more, no less. Everything else is already done and gone on the server - it has forgotten that you even exist unless you store a session (which is basically just a fancy way of persisting the parameters that you pass to the server).
Some people will throw fits here and point out models like VB and C# .NET which appear to persist across multiple pages but really are just clever implementations of the MVC architecture and have gobs of Javascript and a page state model (all of which can be implemented in PHP as well).
I love PHP for its incredible flexibility. I can expand it out nearly as far as .NET if I wanted to with page states, custom tag markup (like in JSP and ASPX), MVC, etc. etc. etc. etc. or I can do <?php echo $x = $y +$z; ?> and be done.
Re: Classes vs. Function Libraries
Posted: Sat Oct 10, 2009 6:41 pm
by Eran
The benefits of using OOP have nothing to do with in-memory persistence. Any long-running program can persist values, whether it's OOP or procedural. There are many benefits and shortcomings for using a shared-nothing architecture that frees the resources used by requests after they complete, but that has nothing to do with the expressiveness of the language. OOP is used to better manage complexity in software design compared to a procedural approach.
And that example, like all basic examples, does little to show the benefits of OOP compared to procedural programming, obviously. The benefits are felt when the application is complex enough that OOP shows its strengths.
Re: Classes vs. Function Libraries
Posted: Sun Oct 11, 2009 11:54 am
by PHPHorizons
pytrin is right on the mark there.
There is one other very important point to mention. Every function name, every constant, every variable that is created in the global namespace is pollution that can conflict with other code. I didn't worry too much about this until I merged a phpBB login system with my site's login. Both applications used $db for the database object.
OMG, that wasn't pretty. Luckily, phpBB didn't name any functions the same as mine (or other constants/variables), because I would have been forced to change my code. Yeah, it's never the other person's application that has to change, it's always yours, because you are using theirs.
Classes provide a built in namespacing mechanism that is very important in php < 5.3. In php >= 5.3, one could negate my argument (to an extent) by using a namespace in their code. This would make things more compatible, but if you have $db, and you bring in code that uses $db into your code. And if that new $db is in the global namespace, you have to remember to prefix each instance of it with a \ or else your $db in the local namespace will hide that global $db. (Not a major problem, hence why my argument is weaker in php >= 5.3, but before then, it's a big deal in many cases)
Re: Classes vs. Function Libraries
Posted: Fri Oct 16, 2009 8:42 pm
by josh
Reddit & digg users man.... youd never see a comment like that on slashdot. Some of the best book recommendations I've seen came from slashdot readers
Re: Classes vs. Function Libraries
Posted: Fri Oct 16, 2009 9:38 pm
by Christopher
volomike wrote:Sure, he was a fan of objects on like the Java platform where objects can be persisted efficiently between pages and sessions, but in the case of PHP, he didn't see the point of that.
Pytrin is correct in why OO is used in PHP. However the sentence above is what exposes the foolishness -- he is a Java guy who cannot understand any other way to do things but the Java way. It would be like a PHP guy saying, "yeah Java has this JVM thing that they have to install on every machine a program runs on ... and then for web apps they need two servers to run them."
Re: Classes vs. Function Libraries
Posted: Fri Oct 16, 2009 11:53 pm
by superdezign
arborint wrote:he is a Java guy who cannot understand any other way to do things but the Java way.
Sort of like an anything-other-than-C/C++-guy trying to do something that is "usually" simple, but is far from it in C++.
/rant
Re: Classes vs. Function Libraries
Posted: Tue Oct 20, 2009 8:07 am
by BDKR
arborint wrote:volomike wrote:Sure, he was a fan of objects on like the Java platform where objects can be persisted efficiently between pages and sessions, but in the case of PHP, he didn't see the point of that.
Pytrin is correct in why OO is used in PHP. However the sentence above is what exposes the foolishness -- he is a Java guy who cannot understand any other way to do things but the Java way. It would be like a PHP guy saying, "yeah Java has this JVM thing that they have to install on every machine a program runs on ... and then for web apps they need two servers to run them."
I think "foolishness" is a bit strong. He's just wearing glasses with a different tint and commenting as such.
Furthermore, his question or concern is reasonable. Rebuilding all the objects you may be using from page view to page view is overhead! Overhead that simply isn't there in his world.
volomike: If you want to use objects,
information can be persisted via something like memcache. You will still need to re-instantiate the objects, but at least some of the work is done for you.
Re: Classes vs. Function Libraries
Posted: Tue Oct 20, 2009 1:25 pm
by Christopher
BDKR wrote:Furthermore, his question or concern is reasonable. Rebuilding all the objects you may be using from page view to page view is overhead! Overhead that simply isn't there in his world.
I said foolish because they do exist in his world -- in the form of Java's application server and persistence frameworks which are much more complex solutions than the equivalents in PHP. One of the reasons that people have moved away from Java to Ruby, PHP, Python, etc. is that Java uses a giant crane to lift everything, whereas other systems have very lightweight solutions (e.g. PHP sessions) for small web apps, but allow you to build various kinds of giant cranes if you need scalability.
Re: Classes vs. Function Libraries
Posted: Tue Oct 20, 2009 3:11 pm
by AbraCadaver
volomike wrote:On Reddit, someone mentioned something interesting. He said that since PHP does not persist objects (at least yet) between pages and sessions, then why do we use them? He asked why we don't just make PHP frameworks that are 100% function based? Sure, he was a fan of objects on like the Java platform where objects can be persisted efficiently between pages and sessions, but in the case of PHP, he didn't see the point of that.
Someone will have to enlighten me. I persist objects across pages and sessions all of the time. PHP doesn't just do it automatically, but why would you want it to?
-Shawn
Re: Classes vs. Function Libraries
Posted: Tue Oct 20, 2009 3:27 pm
by josh
The reddit statement is ignorant because it views objects as buckets of data, and not a programming paradigm for structuring business logic.
Re: Classes vs. Function Libraries
Posted: Tue Oct 20, 2009 4:31 pm
by Christopher
AbraCadaver wrote:Someone will have to enlighten me. I persist objects across pages and sessions all of the time. PHP doesn't just do it automatically, but why would you want it to?
Typically you do this where you have some complexity and initialization overhead. This is often where the objects form a data structure of some kind. A common example in PHP is a shopping cart. A cart is typically a base "cart" data structure that contains a list of "item" data structures. You can store these in arrays in the session and then initialize objects to with the data to create the OO structure -- but it is often easier to just serialize the objects into the session.
Note that this kind of persistence in PHP is different than data that is cached (such as database records), even though they are similar in general goals and concept.