simple explanation PHP OOP vs Procedural? [closed]
Asked Answered
L

5

117

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. What is 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 don't know if that is correct).

Loiret answered 7/10, 2009 at 10:51 Comment(6)
See also: Here are some additional links to related SO posts: #597767 https://mcmap.net/q/189132/-what-are-the-schools-of-oop-closed https://mcmap.net/q/189133/-oop-concepts-confusion stackoverflow.com/questions/500475/learning-oop-well https://mcmap.net/q/189134/-main-concepts-in-oop #1609414 https://mcmap.net/q/189135/-php-real-world-oop-example stackoverflow.com/questions/478773Cruickshank
see also: anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/functions.htmlCruickshank
see also: blog.higher-order.com/blog/2009/04/27/…Cruickshank
seealso: twitter.com/…Cruickshank
Seealso: duckduckgo.com/?q=simple+oop+vs+procedural&ia=web dmid://uu112xheroicsheartyxCruickshank
See also: ardalis.com/encapsulation-boundaries-large-and-smallCruickshank
C
299

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

  1. You want a no-nonsense overview without jargon
  2. You want something that will help you learn from the beginning
  3. 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 No-Jargon Answer:

  1. Many introductory explanations jump quickly into "OOP real world" examples. Those can tend to confuse more than help, so feel free to ignore that for now.
  2. You can think of source code simply as "chunks" of functionality, that just happen to be saved to individual files.
  3. There are different ways of organizing those "chunks"; depending on things like conventions of the programming language, the background and training of the developer(s), or just plain old personal preference.
  4. OOP and Procedural programming are simply two main, generally-recognized methodologies, for how to organize and arrange those "chunks" of code.

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:

    • For example: you could have just one big PHP file with all the functions you have ever written in your entire life, 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 basic string manipulation, functions for processing arrays, functions for file input/output, 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 "chunking" 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 more than one 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, the speed of light, the distance between specific planets, etc.).
    • This is where most OOP explanations get confusing because they branch off into "real world examples" which can quickly get off-topic. Often, "real world" is a euphemism for the ontological perspectives of a particular individual or group. That tends to be useful only once you already understand the concept well enough to teach it to someone else.
    • To understand OOP without confusion, you can skip the "real world" examples for now, and just focus on the code. A Class is simply a way to store functions (aka methods) and properties (aka data) as PHP code in one or more related "chunks" where each individual "chunk" deals with a specific topic or piece of functionality. That's all you need to know in order to get started.
  • 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.

See also

Cruickshank answered 7/10, 2009 at 11:5 Comment(5)
@dreftymac: you could have provided me with feedback about what could be improved in my additions, instead of awkwardly delete it behind my back. Really. I welcome constructive critique, and I invite you to do the same.Perishing
@Perishing OK. Gratitude for the effort, and for taking a moment to make additions. After thinking about it, it seems reasonable that the addition you made merited its own stand-alone answer though.Cruickshank
One addition: A class is also useful for chunking together function data so you don't have to pass it around in parameters.Wilful
+1 especially for your definition of "Framework". I've heard many people try to explain what a framework is, and I think your's is the easiest to understand.Aldarcie
mate... this is the best explanation I have read about oop vs procedural and I have read A LOT! great work really effective, simple and straight to the point. you are right people always use real world examples and it is not a easy way of understanding anything, it is crap!Transmit
C
28

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.

Cortie answered 7/10, 2009 at 11:26 Comment(2)
I find some OOP code to be way more confusing.. try looking into the belly of prestashop for example.Supernal
Great point about Pretashop. Magento is a steaming beast itself, but we decided to remain with it because alternatives such as Pretashop or WooComm are even worse.Aerometry
L
8

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.

Latecomer answered 20/10, 2014 at 20:44 Comment(0)
J
7

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.

Jahdal answered 7/10, 2009 at 10:56 Comment(0)
B
6

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.

Brachyuran answered 20/10, 2014 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.