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
- Which I should learn?
- Whats the difference in code? what are the effects?
- How can a PHP framework help in OOP aproach? (I would like to learn CodeIgniter)
- 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!
Background: You asked for a "simple explanation" which suggests:
Short No-Jargon Answer:
Long No-Jargon Answer:
Procedural vs OOP is just one aspect of a fundamental issue of computer programming: how to make your code easy to understand and a piece of cake to professionally maintain. You can actually write "Procedural" code that follows some of the principles of OOP, so the two are not necessarily opposites.
Your understanding will really grow once you learn other object-oriented programming languages, among which, PHP is a "new kid on the block".
Here is a quick overview of what you learn as you build experience:
A Class can be thought of as a "chunking" of methods and properties
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.
See also
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.
To add on the great answers above. You should see OOP as a natural progression of your coding style -when you start writing small program you might just need to put together a couple of lines of php code, then group them into functions and the more functions you write you may feel the need to better organize them into classes. OOP just let your structure your codes better -allowing a better code maintenance.
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.
Procedural php and oop uses the same php code. Then only difference is that with procedural, you focus on one task and that's it. In oop, you organize your code using patterns or chunks that can be re-used in many different areas of the code.
Simple answer is that, you need to know and understand php. You can learn it at php.net. Once you understand it, then you can start organizing your code in into chucks.
Procedural code uses functions, variables.
Once you get a hang of things, you can start organizing the functions and variables into classes. We start calling the functions as methods and variables as properties.
Good luck.