OOP VS FUNCTION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: OOP VS FUNCTION

Post by bokehman »

Jaxolotl wrote:what do you put on the balance when choosing the scripting 'grammar'.
It's really simple and I don't know why people make this so complicated. Use OOP when you want to reference your variables and methods in object context; otherwise use functions and procedural.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

That's swell, but does not answer the essential question : When do you want to reference your variables and methods in object context?

Most other major languages say: always.
(#10850)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

If you don't think your fully aware of OOP I would suggest going and programming something in a fully OOP language like Java.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I use OOP because it makes for extensible, modular, flexible code. I can write entire apps that literally are driven by 20 to 40 line modules of code. Objects, for me, are easier to spot where your processes are occurring, easier to maintain my code, make logical sense in their layout and design, and they encapsulate processes into smaller, more reusable chunks that allow me to spend more time on coding functionality that organizing my logic.

I have for the most part done away with procedural code as an application development style. However, all code comes down to procedures, and as such, many of my modules, though wrapped in a class method, do still employ procedural code style. But the application itself is not a procedural application.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: OOP VS FUNCTION

Post by Jaxolotl »

bokehman wrote:It's really simple and I don't know why people make this so complicated. Use OOP when you want to reference your variables and methods in object context; otherwise use functions and procedural.
Simple, when someone make a thing more complicated than what is should be is because he/she ignores the nature of the thing.

About recycling code my way of writing is making a library with the most used basic funtions (db connection, math operations, data process, ecc) and an "ad-hoc" library with the functions needed specifically for the current project and with some special funtions that combine a branch of the standard functions into a big one. Just like assambling many transistors on a chip.
Doing this I made a lot of projects that allows me to update them without mayor problems.

Many things depends on your "inside" logic, you may do the same thing in 5 lines or in 50, everything is about how intelligent you are when conceptualizing the project and the data flow, and how "consistent" is the dictionary of the choosing language you have on your "fingers".(anyway there is always a manual around)

I know there's another way to code that most of the best programmers describe as kinda Top Level and I'm really curious.

For example (this is a theory based on what I read this days about OOP) a Java or Pyton object may stay alive for a long time on the "system" and this is and advantage if you need it not to start and die everytime. But on a PHP web based project thie object may have a life time of minutes or seconds. So which part of the code will take advantage of OOP and wich one will not?

I just like : "you can put on your boots or your sport shoes to walk for 100m, for 500m you'll notice the difference, for 2km you'll start thinkin'bout' sport shoes, for 10km you'll cry for a pair of sport shoes..... to sit down you may use one shoe and one boot.... with sport shoes you may use the left one on the right foot an so on for a while, but don't think about doing that with boots"

Which advantage will I take if I use OOP on for example db connection over procedural? or may be If I use OOP I should think the whole thing in a different way...because it'll be stupid to use OOP but still thinking in a procedural way.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

For example (this is a theory based on what I read this days about OOP) a Java or Python object may stay alive for a long time on the "system" and this is and advantage if you need it not to start and die everytime. But on a PHP web based project thie object may have a life time of minutes or seconds. So which part of the code will take advantage of OOP and wich one will not?
That thought has occurred to me too.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Buddha443556 wrote:
For example (this is a theory based on what I read this days about OOP) a Java or Python object may stay alive for a long time on the "system" and this is and advantage if you need it not to start and die everytime. But on a PHP web based project thie object may have a life time of minutes or seconds. So which part of the code will take advantage of OOP and wich one will not?
That thought has occurred to me too.
First, I really don't see any design link between persistence and OOP. You can persist data and objects in many languages to many different kinds of datastores. But persistence mechanisms are an impelemetation detail and generally irrelevant to the design of the system. They should be behind a clear interface or dealt with outside the code.

However there is another point here which may be causing some confusion, which is the "share-nothing" architecure that PHP uses/promotes. Java is a classic example of a language that uses/promotes custom solutions to specific problems like persistence where it implements its own datastore. PHP on the other hand provide a persistence framework and base implementation that uses the filesystem as the datastore, but allows you to pretty easily implement other common datastores (database, etc.). But the important conecept is that PHP promotes using stantard componets (e.g. databases, filesystem, LDAP, etc.) to do the work that in something like Java they build custom solutions for. Neither is better or worse, just different, but you need to understand the difference or you can easily misunderstand when comparing them.

For example, if you want an object in PHP to "stay alive for a long time" you would do:

Code: Select all

include "MyClass.php";
session_start();
$myobj = new MyClass();
$_SESSION['myobj'] = $myobj;
And to retrieve it:

Code: Select all

include "MyClass.php";
session_start();
$myobj = $_SESSION['myobj'];
If you use _autoload() you do need the include. If you implement a Session or Service Locator class then the code can be even cleaner. But in general making and object "stay alive for a long time"" is pretty trivial in PHP.
(#10850)
Post Reply