Jump to content

Archived

This topic is now archived and is closed to further replies.

TheBrandon

Procedural vs OOP

Recommended Posts

Hello all,

 

I'm about to try a project a bit beyond applications I've made before and I'm trying to expand and understand the process for writing a big application with a ton of functionality.

 

I've gone through all of the actions and tried to condense them down as best as I can and plan as best as I can.

 

However before I start writing code... I'm curious if this is a case for OOP programming?

 

I have read a lot on OOP programming (and watched the tutorial stickied in this forum) but I still don't fully grasp its practical application outside of reusable code. I currently have a few classes I wrote and use (mostly for form validation or database connectivity) but I am having trouble learning the difference in something like form validation and having OOP handle "objects" like a user.

 

Right now I have 3 huge "parts" of the website: users, projects and a forum. It's for managing projects at a company I work for.

 

So for example the users section; register, forgot password, check access level, if they are an admin, etc. should there be a user class that contains all of this code? In what way is this better than simply having a users_functions.php file with all of the functions pertaining to users in it?

 

Has anyone ever written a user class I could look at to see if I get it? Or should this simply be procedural?

 

What questions should a programmer ask themselves when deciding between procedural or OOP?

Share this post


Link to post
Share on other sites

You can as easily write the application in procedural as you can in OOP, it's all just a matter of preference and opinion.

Share this post


Link to post
Share on other sites

The main thing about oop is that the methods (public and private interface) to the internal data is bound together with the actual data.  That can be beneficial in organizing your data.  There are also common design patterns that have proven to be good ways to solve common problems.  This is one of the reasons so many web application frameworks include an implementation of the MVC pattern.  In php oop is not as often used for its inheritance properties, other than in the case of well known libraries that have been written in oop.  Often the oop is hiding complexity and again the importance is that the public methods are providing an api for use of that class.

 

I wouldn't underestimate the value of private or protected variables (or even just leaving them public) in a class defination.  When you consider users, it's nice to be able to have a let's say, an array of "users" which might represent "friends", or an array of permissions.  The functions might be very similar, but it certainly helps organize your data and keep things clean, when you can have a method that for example,  reads in the permissions for a new user and stores them in the user object. 

Share this post


Link to post
Share on other sites

If this is going to be a big project with multiple people working on it then you should def use OOP... Over all I find OOP to be a little more work in the end but the organization it brings is work every bit of the extra work.  Another great feature of it is so you don't need to make a whole new separate function for just a little change on a current function you already have.

Share this post


Link to post
Share on other sites

It goes without saying OOP is far superior for large projects. Even if JUST the only benefit was reusable code. Imagine you had been doing OOP from the beginning, you would no doubt already have a database abstraction layer, classes for abstract user management, common validation, routing etc etc

 

Think about how much time this would take you if all you really had to do was extend these abstract classes to create specific functionality for your app. It would take you half the time.

 

On top of that, your code is more manageable, maintainable, readable, extend-able, portable etc etc

 

If you're new to OOP then it will obviously take you a little longer due to the learning curve but when you truly grasp what is going on you will see the HUGE benefit. It is definitely not down to preference and opinion as noted above. The fact of how more beneficial and proven this design is than procedural is just that, fact.

 

I suggest you check out the Zend Framework. If you do move forward with OOP I would probably try to use a framework instead of mashing together your own.

Share this post


Link to post
Share on other sites

OOP of course. You just can't make flexible, portable, or reusable applications without  inheritance, polymorphism and encapsulation.

 

And if multiple people are going to be working on that app that OOP is the only way. Without objects in general you are walking in the dark. OOP is made because people wanted to translate something that is easy to understand by the humans and that are OBJECTS. Procedural programming will be only effective in case of writing assembly language but there are also some ways to use OOP principles so i would definitively.

Share this post


Link to post
Share on other sites
You just can't make flexible, portable, or reusable applications without  inheritance, polymorphism and encapsulation.

 

So procedural isn't portable? or re-usable?

 

And if multiple people are going to be working on that app that OOP is the only way.

 

Companies still work with dozens of people on projects with procedural coding, case in point: Drupal

 

Without objects in general you are walking in the dark.

 

Anyone without a clear idea is.. OO makes it not clearer.

 

OOP is made because people wanted to translate something that is easy to understand by the humans and that are OBJECTS.

 

Only robots understood procedural?

Share this post


Link to post
Share on other sites

I don't know what are you doing man. But procedural will never be as portable or flexible or anything like OOP. Maybe if you are on some drugs and 24/7 in front computer then its ok. I mean we can even understand binary but who will write binary code. Idea is that machine works for you not you for the machine.

Share this post


Link to post
Share on other sites
I mean we can even understand binary but who will write binary code.

 

011100000111010101101110001000000110100101101110011101000110010101101110011001000110010101100100

 

I guess I do.. :P

 

Theirs no need to insult/mock me I only questioned/pointed out a few things in your text.

Share this post


Link to post
Share on other sites

well i don't know if you understand it or not but i do. you just can't admit yourself that oop is better because you are using it and if you do admit that would mean that you spent a lot of hours not doing very much with procedural so this discussion can't continue normally.

Share this post


Link to post
Share on other sites

well i don't know if you understand it or not but i do. you just can't admit yourself that oop is better because you are using it and if you do admit that would mean that you spent a lot of hours not doing very much with procedural so this discussion can't continue normally.

 

lol wtf are you smoking? :smoker:

 

In any case, both OOP and procedural have their own place in the world. In this case, it would probably be better to use OOP, but again it's a matter of opinion.

Share this post


Link to post
Share on other sites
but the ones who chooses OOP are smarter ones

 

Typical "If all you have is a hammer, everything looks like a nail." behavior.

Share this post


Link to post
Share on other sites

yes you are right. it is matter of opinion. but the ones who chooses OOP are smarter ones :)

 

Now you're just trolling, aren't you?

 

Edited for clarity.

Share this post


Link to post
Share on other sites

Are we seriously going to pit OOP against procedural? There is no competition, the benefits of OOP far outweigh the drawbacks. They both have their place, but if you're coding procedural on large projects I feel sorry for you. That's torture. If you're experienced and you STILL insist on using procedural for every application then I would consider studying programming in general.

 

There is a reason OOP was the main focus during the development of PHP5.

 

Drupal is taking a new direction:

 

http://blog.opensourcenetwork.eu/blog/programming/drupal-7-turns-oop

 

@Pickachu2000 LOVE that video too! Saw it the other week. PLEASE make more!

Share this post


Link to post
Share on other sites

 

From the article:

 

Basically the only real object-oriented piece of Drupal 7 so far is the database layer (containing classes with abvious names like Database and Query).

 

and ..

 

But it seems that Drupal takes it up against the large majority of programmers that find OOP the best way to go.

 

I'm not saying OOP is better then procedural or vice versa. As a programmer you should be open to all ways of programming, you can learn a lot from functional programming to apply in OO designs. OO also doesn't satisfy all cases like eg multicore programming.

Share this post


Link to post
Share on other sites

Ok man now seriously. I understand you favour procedural. I can't see any reason why would i do procedural in php. I thinking now why would i do that and i don't come up with anything. On the other hand when i think why i am using OOP i realize that with polymorphism, inheritance, design patterns i can easy reuse my code and code is easy to adopt to changes. For example if you make a good interface you can switch through implementations without problems. Now this doesn't mean that OOP is the best way it REALLY does mean that is the best way that I KNOW. So please tell me what you think are the greatest benefits of procedural coding in contrast to OOP. I am really trying but i just don't know.

Share this post


Link to post
Share on other sites

 

From the article:

 

Basically the only real object-oriented piece of Drupal 7 so far is the database layer (containing classes with abvious names like Database and Query).

 

and ..

 

But it seems that Drupal takes it up against the large majority of programmers that find OOP the best way to go.

 

I'm not saying OOP is better then procedural or vice versa. As a programmer you should be open to all ways of programming, you can learn a lot from functional programming to apply in OO designs. OO also doesn't satisfy all cases like eg multicore programming.

 

I said they're taking a new direction. And they are; they used to say they would NEVER go over to OOP. But guess what? They have *started* to. End of the article reads:

 

Does Drupal 7 turn OOP? No!

 

I am in no way saying it is OOP. And to say "As a programmer you should be open to all ways of programming" is exactly right. Thing is, we start with procedural (php programmers) and move onto OOP. If we were programming java we would be using OOP from the start. As I say, there is a reason PHP5 had a very strong focus on bringing it up to date with facilitating OOP.

 

Procedural has it's place alright, but from a practical point of view it is almost always less beneficial. I can think of one benefit of procedural - faster. You could say simpler but it's really not at all. Using an API is far simpler.

Share this post


Link to post
Share on other sites

ignace understands OOP concepts allot better than most people around here. All he is saying is it is not the only valid option and in fact, sometimes it's not required.

 

If you think otherwise it is likely you yourself don't understand the concepts well enough to keep your eyes open to other solutions.

 

This happens to allot of people once they have a decent grasp of OOP. They consider that it MUST from now on be used for everything. This is not the case at all.

Share this post


Link to post
Share on other sites

ok @thorpe if you think like that then please tell us what are those situations. no one here is giving us anything to understand when procedural is better. i gave you mine reasons why OOP is better. would someone of you that think that OOP is not the only way give us some examples already. Thank you.

Share this post


Link to post
Share on other sites

ok @thorpe if you think like that then please tell us what are those situations. no one here is giving us anything to understand when procedural is better. i gave you mine reasons why OOP is better. would someone of you that think that OOP is not the only way give us some examples already. Thank you.

 

Prototyping, small applications, and applications built for pure speed all could have an advantage using procedural over object oriented.

 

Here is an example, since you oh-so-badly want one where oop isn't really worth it... and don't tell me you'd rather do the object oriented version because that is a load of BS.

 

<?php
echo "Hi there!";
?>

 

vs.

 


<?php
class speak {
public function sayHello( ) {
	return "Hi There";
}
}
$talking = new Speak;
echo $talking->sayHello();
?>

 

 

Oh and..

would someone of you that think that OOP is not the only way give us some examples already.

You're saying OOP is the only way to do things? Way to think outside the box!

Share this post


Link to post
Share on other sites

Anyone else think this debate is quite 'sad'? Procedural and OOP both have their places. If it can be done in procedural, doesn't mean it should and vice versa.

 

Problem here is people are saying procedural has it's place but it's such a small application that it's trivial. Prototyping can be improved by using a prototyping framework. Could that framework be done in procedural code? Sure, but functioning isn't everything.

 

The two sides:

 

http://www.linuxjournal.com/content/programmer%E2%80%99s-discussion-procedural-vs-oo

http://www.virtuosimedia.com/dev/php/procedural-vs-object-oriented-programming-oop

 

As we know (and from the first link above), this is an endless debate. If you use procedural code in large applications (and I mean exclusively) then you clearly have not programmed in any other language than php. Get a general programming book.

 

 

Share this post


Link to post
Share on other sites
no one here is giving us anything to understand when procedural is better.

 

I don't believe anyone ever said it was better.

 

If you use procedural code in large applications (and I mean exclusively) then you clearly have not programmed in any other language than php. Get a general programming book.

 

I'm not sure your in a position to hazard guesses at what languages people here have and haven't used.

 

So... what about the Linux kernel? One of (if not the single biggest) open source projects around.

Share this post


Link to post
Share on other sites

×
×
  • 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.