|procedural vs object oriented|
which programming type is the most recommended for php
i am more familiar with php procedural programming. i know it has also an object oriented interface. since i am not familiar with the objected oriented, i am affraid to use it, but would however ask which programming type is the most recommended by the PHP group. Also which one is the most matured one?
Most hard core PHP coders I've talked to say OOP, all the way. It **forces** you to compartmentalize your code, create classes, and makes your code portable as libraries for other unrelated programs. But . . . you can do this anyway, without creating classes and respective objects.
However, in my experience (just an opinion, mind you) strict OOP makes for more code, larger scripts, and is not always as efficient. For example, a simple mailer or a simple function shouldn't require including three files of classes. I have the same problem with Perl libraries - why include 1500 lines of code if all I'm doing is a simple read and parse, which I can write up in 20 lines of code or less.
The other problem with OOP (and perl modules) is often, the classes are a "black box" - you pass values to it and hope it's programmed correctly. When things go wrong, it's a mess to sort out.
Personally I'm on the fence. Some large projects I've worked with were strict OOP, others, a mix of both.
Here's a great, and fairly short, recent discussion [webmasterworld.com] of PHP and OOP.
I too am on the fence, I try and try to come up with a good reason to change my codebase over to OOP but in my experience it does make for larger more complex scripts.
I wouldn't claim to be knowledgeable enough to call OOP a fad but sometimes it can seem that way with all the zealotry. The main thing for me is to get the code out and make sure it works. I can learn the advanced techniques as I go. That reminds me of a very good article [norvig.com...] The key part to this discussion is the last part about choosing a language Appendix: Language Choice.
Trying to box yourself in to one style or another seems misguided, it is like anything else use the right tool for the job.
I have read some very good articles and tutorials on creation of OOP code and at the time they make complete sense. Once I start to implement them that is another story.
I use functions a lot and IMHO I think that is a mix between OOP and procedural, I could be wrong. I can see the theory behind OOP makes sense in certain situations, I just haven't reached that AH-HA moment yet.
Now before I get spanked by the OOP guys, I should also state that the way my brain works is not objectively ;) I work in a very linear fashion, if this, then this, else do this which helps me to keep focused.
I personally am still working towards a good understanding of OOP in PHP but mainly because the fundamentals can carry over to other languages and I see that as being beneficial. I have a solid understanding of the PHP syntax and can build on an already good foundation to learn higher level programming techniques.
So in the end I think it comes down to what you feel most comfortable using, whether it's OOP, procedural or a mix of both.
OOP almost always. Though, yeah small snippets of procedural stuff I usually dump into a "functions.php" file.
I personally find it a mess though to have tons of procedures and various mishmashes of global variables floating around. Even with good comments, it's a pain to remember which functions rely on which other functions etc. when I need to copy that functionality to another site.
In any case, I try to make functions fully self contained, if they can't be I write classes that contain all the functions I need instead. If what I'm trying to represent, has many functions that are going to operate on it, I put it in a class.
My main reasons are organization, maintenance, and portability. Personally OOP helps me do that better.
The way I started was to do a particular thing procedurally. Then when I finished, I'd take the whole thing and rewrite it as a class. Eventually it became habit, and I just wrote classes instead.
Imagine if your computer had no case or sockets, instead of defined I/O, you just had a ton of wires sticking out. Each time you switched your keyboard out, you'd have to make damn sure you new which wires went where. OOP on the other hand puts it all in a case and says Computer->Keyboard->here. OOP just makes things much easier to work with.