Jump to content

When to use OOP?


phpfreak101

Recommended Posts

I'm currently a freshmen in college, majoring in Computer Science.  We've been going over Object Oriented Programming in Java the last 2 semesters and I'd like to take take what I've learned and apply it to web programming (specifically php) that I learned on my own.  I'm just a little confused as to when to use OOP.  What situations or methods would be good to create as an object for web programming?  What areas of dynamic websites (database driven) do you find most useful to build as objects, rather than building them as traditional functions/methods.

Link to comment
Share on other sites

A lot of it is down to experience.  You'll learn over time where objects are appropriate and where they aren't.

 

Too much abstraction and you'll have code that spends more time dealing with the OOP framework than actually doing the task it's supposed to.  Too little and your code will be difficult to debug, maintain and re-use.

 

A situation where OOP works well is Smarty templates.  Another is the SimpleXML object in php 5.

Link to comment
Share on other sites

I would go with daniel0

 

If you can turn it into a class( perhaps be refering to obejcts you haven't really grasped the core of OOP)  to be exact then do so. You never know where or when these may be useful for other applicaions.

 

PersonallyI have classes for form generation date, handling, database access as standard and these are imported automastically to every website I develop and boy does it save bucket loads of work. I'm currently working on a simialr routine for dynamic style sheets, but it's  a way off yet.

 

You know the service you want to provide, so you build the tools you need to provide that service, hence the expression a poor workman always balmes his tools. Rememebr he made the tools.

Link to comment
Share on other sites

The bottom line with functions/objects/loose code is: If you use it once, it should be loose, if you use it a lot and it's static, use a function, if it's the core of your software it should be an object. The main idea is to keep away from using objects for everything. MediaWiki is a great example of OOP php.

 

Software actually runs faster with no classes and only functions, but it can become impossible to maintain. So you're balancing speed and maintenance.

Link to comment
Share on other sites

Too much abstraction and you'll have code that spends more time dealing with the OOP framework than actually doing the task it's supposed to.  Too little and your code will be difficult to debug, maintain and re-use.

 

Good advice there.

 

As dumb as this sounds, use OOP when you're creating objects or things that have object-like behavior.  For instance, employees, jobs, parts of a framework, etc. are all objects; they are things you can imagine holding in your hand.  A library of common functions, such as disk I/O, can be an object, but chances are you don't want to have to create a new object (or use an existing one) every time you want to check if a file exists.  These types of utility functions are best organized under a namespace to avoid naming collisions; it just so happens that the only way to do so in PHP is with the class keyword.

Link to comment
Share on other sites

Software actually runs faster with no classes and only functions, but it can become impossible to maintain. So you're balancing speed and maintenance.

 

Software runs faster without functions too.

 

Remember that developer cycles can be more expensive than server cycles.  Focusing too much on the fastest code execution can cost you more money than buying a faster server.

 

There is a maxim in software development that 90% of development time is spent on maintenance and extension, and these are the areas where a well-designed object-oriented application will shine.  Few things are as awful as badly designed OOP, so stay away from a major OOP project until you are confident you understand design principles.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.