« Return to Blog
Procedural PHP vs OOP PHP with Class and Framework - Easy and Best Explanation
Estimated reading time: 3 minutes, 52 seconds
If you would like to learn PHP and want to get an Idea about OOP vs Procedural and you have read many other blogs and tutorials about OOP vs Procedural but you still can't understand the approach then this is the best explanation on it. With special thanks to Dreftymac for writing it.
Background: You asked for a "simple explanation" which suggests:
- You want a no-nonsense overview without jargon
- You want something that will help you learn from the beginning
- You have discovered that no two people ever answer the question the same way, and it's confusing. That's the reason you are here asking for a simple explanation, yes?
Short Answer: If the above sounds right to you, the right start consists of:
- 1) any beginning resource on PHP programming; and
- 2) experienced programmers with whom you can do routine code review; and
- 3) access to Stack Overflow
Longer 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 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.
You really will not get a complete understanding until you actually start digging in. 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:
- 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 be "chunked" together, and there are different ways of doing this:
- For example: you could have just one big PHP file with all the functions you have ever written, listed in alphabetical order by function name
- For example: you could have multiple PHP files with functions that are chunked together by subject matter [e.g., functions for doing math, functions for doing encryption, 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 can be thought of as a "chunked grouping" of methods and properties
- methods are simply functions that are logically related to one another in some meaningful way. The words "method" and "function" are basically two different terms for the same thing.
- properties are simply data values that are related to the class. These are values that are intentionally non-isolated to any individual function, because all of the functions in the class should have access to them.
- For example: if your class has a bunch of methods for doing astronomy, properties of the class might be the values for certain famous numbers that all astronomy methods need to know about, (like PI or the speed of light or the distance between specific planets or whatever).
- this is where most OOP explanations get confusing because they branch off into "real world examples" which can quickly get off-topic.
- To understand OOP without confusion, you can avoid the "real world" examples. A Class is simply a way to store functions (aka methods) and properties (aka data) as PHP code in one or more related files where each individual file deals with a specific topic or piece of functionality. That's all you need to know.
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 ita "Framework".
- A Framework is just the next-highest level of "chunking" (including coding style and conventions) 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.
Now, from this point, I hope you will have a very clear idea regarding what is this all about. You can further read comments about this here.