Jump to content

Looking for simple OOP examples that demonstrate utility over straight functions


Recommended Posts

I have gone through a lot of the threads in this forum, and several tutorials online, and while I feel comfortable with the syntax and instantiation of classes, I still don't understand their utility as opposed to using straight functions.

 

Therefore, I was wondering if some of you would be willing to post some real-world, short, simple OOP examples that demonstrates their utility OVER functions?

 

Regardless, thank you for your time and consideration.

 

cgm225

You have to think of OOP as a long-term investment in to an application. The planning and the beginnings are the toughest, and projects evolve while you're developing them. If you're programming procedurally, you are developing as you are solving the problem in your head. I think, and others may disagree, that true OO programming requires the problem/excersize to be solved BEFORE you start coding.

 

Maintaining procedural/functional code is tough. An example; If you have a popular function which is used everywhere, there may be serious side effects if along the line you have to change it. That's a lot of code you'll have to retest, modify, etc.

 

To understand and it's practicalities, it's best to start learning along with review of design patterns. Here's a good start:

http://www.cs.wustl.edu/~schmidt/PDF/patterns-intro4.pdf

 

Maybe someone else will chime in with more of what you're looking for.

 

Good luck :)

 

 

I believe that OO PHP is a difference between a decent programed and a great programmer. A great programmer makes things simple, and reusable. That is what exacly  OOP provides for all us programmers.

 

Sitepoint does a very straight forward easy to understand tutorial on OO PHP specifically.

 

http://www.sitepoint.com/article/php-paging-result-sets

 

Enjoy!

It would depend on the size of the application. In larger applications OOP is the way to go for many reasons. I'm sure you can find out about all the benefits of using it on google ;)

 

In smaller, 1 or 2 page, applications (perhaps just a simple guestbook) You wouldn't need to use it. It would probably be better in those case to have some procedural code instead of OO code just because the applications are so small.

 

One problem I've found with a lot of OO tutorials (in general) is that they are always talking about how bananas and apples are a type of fruit. This isn't really useful for programmers for a couple of reasons: 1, they don't know what fruit is (;)) and 2, It's damned obvious that banana's and apples are fruit.

 

But think of this: You have a forum, and in that forum you have "users". These are people who use the forum. (banana's are fruit!). Whenever you want to find out a user's name you could have a function called "find_users_name($userID);" which would do all the work. Or, you could have a user object and  use that "$user->name";

 

You might have a page on this forum which displays all the users. You could, again, find out how many users there are, loop until you've shown them all and be happy. Or, you could have an object that does it for you. instead of

$variable=get_max_users();
while($i<$variable){
echo(find_users_name($i));
$i++;
}

you could have

$userCollection->printList();

But, as I say, it really does depend on the size of the application or the size of the problem you're trying to solve. And the more you use it the more you'll like it ;).

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.