views:

939

answers:

4

Hi,

I would like to learn PHP and want to get an Idea about OOP and Procedural. I read some other blogs and tutorials about OOP vs Procedural but I still can't understand the approach.

OOP vs Procedural

  1. Which I should learn?
  2. Whats the difference in code? what are the effects?
  3. How can a PHP framework help in OOP aproach? (I would like to learn CodeIgniter)
  4. Does procedural need a Framework?

I really want to know the code difference of the both, my understanding of OOP is you create a class like and it can be access. (I dunno if thats correct).

Thanks!

+1  A: 

You should learn both. Objects are just one of the many possible abstractions in existence, and abstraction is what programming is ultimately all about. That said, start with procedural stuff, and then add objects later, because PHP objects' internals are procedural anyway.

As for frameworks; first learn the fundamentals of the language, write throwaway experimental programs and such. Later you can familiarize yourself with frameworks and consider yourself whether you find some of them useful in some context. They definitely aren't mandatory.

Joonas Pulakka
+9  A: 

Background: You asked for a "simple explanation" which suggests three things:

  • You want a no-nonsense overview without jargon
  • You want something that will help you learn from the beginning
  • Your ultimate goal is to write some good PHP, and the OOP vs procedural stuff is a stepping-stone to get you there, not necessarily your end destination.

Short Answer: If the above sounds right to you, the short answer is to find: 1) any beginning book on PHP programming; and 2) an experienced human being programmer who is physically close to you who is willing to actually sit down with you once and a while and review good coding practices; and 3) access to a site like Stack Overflow when you have questions that cannot be answered by 1) or 2) above.

Answer: Procedural vs OOP is one of the topics that no two people will answer exactly the same way, and it can be confusing. You have already found this out, and it's the reason you are here asking for a simple explanation, yes?

Here is a no-jargon explanation. Procedural vs OOP just deals with how you can write code in such a way that the different pieces are easy to understand and maintain. You can actually write "Procedural" code that follows some of the principles of OOP, so the two are not necessarily opposites.

You really will not get a usable understanding until you actually start digging in, because the terminology and "jargon" is just too thick in this particular forest. Here is a quick overview though:

  • You can write PHP that does useful tasks
  • You can organize useful tasks into "chunks" of code
  • Sometimes those "chunks" of code will behave differently based on parameters you pass in
  • Chunks of code that accept parameters are called "Functions"
  • Functions can also be "chunked" together, and there are different ways of doing this ** you could have just one big PHP file with all the functions you have ever written listed in alphabetical order ** you could have multiple PHP files with functions that are chunked together by subject matter (e.g., functions for doing math, functions for checking spelling, etc)
  • OOP is a special way of "chunking" Functions together into a "Class"
  • A Class is just another level of "chunking" code together so that you can treat it as a unified whole
  • A Class is useful because it allows you to organize your code at a very high level in a way that makes it easy for you to understand, use, and maintain
  • When someone has written a lot of functions, and organized them into a lot of Classes, and gotten those to work together in some cool way, they package the whole thing together and call it a "Framework"
  • A Framework is just a system of coding style and practices that one or more people agree on because they like the way the code is organized and it suits their working style, preferences, values, or whatever.
dreftymac
Thanks Dreftymac, Great Answer! You nailed it. A Very clear explanation, Especially the bullets. Thanks!
Pennf0lio
i wish i saw this answer a 2 years ago when i was struggling. thanks for taking the time.
centr0
+2  A: 

OOP is nothing more than a design pattern. If you're just beginning then learn the basics by focusing on the procedural approach. Most importantly, get familiar with basic principles like loops, conditions and calling other procedures.

While you're creating your procedural code, make a habit by adding related methods inside a single source file. Learn to divide your procedures into logical units and then you're already starting to become object-oriented. Basically, an object is nothing more than a collection of methods that are related to one another simply because they operate on the same set of data. (Not speaking of databases here, but application data!)

OO is mainly used to make your code more logical by dividing everything in simple blocks. By combining the right blocks, you get a complete application. OO isn't a silver bullet or golden hammer which will solve all your problems. But what it does do, is making your code easier to understand.

Then again, some people still manage to make a complete mess out of objects, simply by turning them into huge super-objects with hundreds of methods. Such objects don't differ much from a regular procedural approach, simply because of the huge amount of methods being combined together without any real logic. It's a mistake that's easy to make when people start doing OOP too fast.

Workshop Alex
You have a point. Thanks!
Pennf0lio