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
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 don't know if that is correct).
In fact, PHP was initially developed as a procedural language and only later extended to OOP. PHP programmers cannot agree on which style is preferable. For me, the most effective method of programming is simply a matter of opinion, the demands of the project, and experience level.
Object-oriented programming is based on the real world. Procedural programming is used for designing medium-sized programs. Object-oriented programming is used for designing large and complex programs. Procedural programming uses the concept of procedure abstraction.
Security: Object-oriented programming is more secure than procedural programming, because of the level of abstraction or we can say data hiding property. It limits the access of data to the member functions of the same class. While there is no such data hiding in the procedural programming paradigm.
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 will learn as you build experience:
You can write PHP source code that does useful tasks
You can organize useful tasks into "chunks" of code
You can think of "chunks" of code independently of the individual files where they are saved
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:
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 "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.
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 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, plans for world domination, etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With