Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Object Oriented or Precedural?

TopSilver

Bronze Coder
Would you rather code in object oriented or procedural. Xenforo 2 is object oriented mainly but other platforms such as wordpress or wordpress add-ons I hear are procedural. Which one do you prefer more and why? Personally I prefer object oriented using class based methods. It seems to be more interesting to me and much easier to learn in my personal opinion. Sometimes when I read books I find it hard to understand procedural methods in the way they explain the code. The syntax isn't hard but the way the code is explained as only procedural.

Yet when you dive into the code for object oriented you can basically see what's going on a lot easier. I'm not professional at either and still learning but it's more-so simpler to grasp personally for myself.

Which do you prefer?
 
I prefer OOP (object orientated programming) because it just seems a lot more organized and modular because of the class system and extensions.
For example, I usually create a class called 'System' in my projects. This class connects to the database, handles error reporting & logging, and also supplies some system wide functions that are used by all the other classes.

So, I can then use the System class with others...
Example:
class User extends System{ }
This allows the User class to inherit functions/variables from System! :)

However, I also use procedural for smaller projects. Sometimes there's just no need to have an organized class system if the pages I am creating are very simple and not going to be expanded upon. If I need to write something up quickly, I'll just create a single functions file that holds them all in one place. If I need to write something REALLY quickly, I'll just write everything inline, but I hardly ever do that because 9 times out of 10 it looks disorganized and results in more code because you can't reuse things. So, if you're coding in almost any language and you have to choose OOP or Procedural, at least do one of them instead of creating spaghetti code with no defined functions/methods/classes. They're both better than nothing, but personally I use OOP around 90% of the time these days.
 
For me it really depends on how performance-critical the code in question is.
Especially in the context of PHP and other CGI webapps, you have to keep in mind that all your code runs every time on each request.
With huge frameworks like Symfony that means constructing thousands of class instances on each request, which might exhaust your memory and/or CPU sooner.
 
Well, I don't really have a preference much to say the least.

I quite like both ways of Programming but that's just as of now and I've yet to change as I start to learn more Programming-Languages and further study Computer-Science. C which is my new favourite Language is a Procedural-Language. So, there are no Classes. Now, I'm not good with Classes and so I plan to further study them when I get a new Java Book. So, that's why I quite like Procedural. Instead in C, we use Functions(Or 'Methods' as they're called in Object-Oriented[I'm pretty sure anyway. Again, I've yet to further study them]).

As for Object-Oriented, I don't really have a solid opinion on it, just like Procedural-Programming. What I don't like is Classes, Interfaces and Inheritance. Maybe that's because I don't know much about them yet and have still to continue learning them as that more than likely, is the case. But, I do happen to like Java and that is Object-Oriented. I have also picked up a tiny bit of interest in Ruby. That's also Object-Oriented. Python I know a bit about but it's not my cup of tea. That's also Object-Oriented. Again, it's things like Classes and Inheritance that I just don't happen to get which is why I don't have a solid opinion on Object-Oriented.

As of now, again, I've yet to further learn about Programming-Paradigms. Then once I learn a bit more about them, I learn about Classes and Inheritance and all of that, then maybe I'll have a preference for a Paradigm.
 
I prefer OOP ( Object Orientated Programming ) as i feel its more structured, coming from the old ways many years ago when PHP was more spaghetti code and the dreaded functions files linking structure was a daunting task.
With OOP design pattern now with frameworks or starting from scratch the code is more organized so its easy to find what and where needs editing.

I try always to group my code into an MVC structure too keeping the model classes, controllers, configurations and view files separate to keep it again organized so if having yourself and many other developers it wont be hard to jump straight back in and continue on.

OOP over old speggetti code, OOP and MVC wins now everytime for me.
 
Thanks for the question. I feel you can spend more time on the logic this way. It's good to start the other way though for training purposes.

I agree with this. I seem to get most of my training trying to code a Xenforo 2 add-on and it gives me much more logic that I'm able to work with. Seems to be the best option for me to start like that.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom