Classes vs. Function Libraries

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
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Classes vs. Function Libraries

Post 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.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Classes vs. Function Libraries

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Classes vs. Function Libraries

Post 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.
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Classes vs. Function Libraries

Post 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)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Classes vs. Function Libraries

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Classes vs. Function Libraries

Post 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."
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Classes vs. Function Libraries

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: Classes vs. Function Libraries

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Classes vs. Function Libraries

Post 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.
(#10850)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Classes vs. Function Libraries

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Classes vs. Function Libraries

Post by josh »

The reddit statement is ignorant because it views objects as buckets of data, and not a programming paradigm for structuring business logic.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Classes vs. Function Libraries

Post 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.
(#10850)
Post Reply