It appears that the use of OOP in PHP is not always advocated. Many projects in PHP use OOP extensively (such as CakePHP) while others don't use it at all (Drupal, php.net). So I was wondering what is the general consensus on this matter; when is it best use OOP in PHP and when is it not?
I just asked a similar question and got some good answers, here is the thread --->>>http://forums.digitalpoint.com/showthread.php?t=283611 Maybe you can benefit from the answers I received as well.
Those responses are good for your particular question, but I have used OOP extensively in C++. I'm not really asking what OOP is, but rather when is it more appropriate to use OOP in PHP. From my own experience with OOP in PHP, it seems a little under developed (prominently in PHP 4), and the fact that all PHP code is evaluated per page request, it makes OOP seem even less efficient thus less appealing. Following the usual coding style employed by many OOP-based languages, one would put each class in it's own file (for example, class "box" would go in a file called "box.class"). However, loading each class from separate files in PHP seems very inefficient. I want to know how these drawbacks affect OOP coding in PHP and what other's opinions are on this matter.
In my opinion, the larger the project is, the easier it is to handle with OOPHP. If you have a small site or set of scripts, it is not going to matter much which way you choose, just choose what you are more comfortable with.
thing with OOP is that you can develop a library of reusable classes for other scripts so if you planning on writing other stuff then it becomes more time efficient. If you just writing a small script then it may be a waste of time.
Generally if you plan to make an extensive framework of some sort you should use OOP, if you're coding a contact form, then don't ..... Its up to the individual, as you know from using c++ OOP code seems to take more of a manageable structure to that of procedural style code, it somehow seems easier to maintain....
It's like CSS but you can use inline styles except for classes. I like to play (and I'm still learning) OOP and MVC. It's really great to know them and use them when needed. It's about reusable code and site structure but like krakjoe said, if you're coding a contact form ... play with functions better.
Thanks for the responses. I've been playing around more with OOP in PHP and it doesn't seem to be as slow as I had previously thought. So I'll probably end up using as you all suggested--in larger, extensible projects where it's necessary.
OOP is far easier to manipulate for larger projects i find. And easier to plug in extra features. Generally speaking i still make it quite interfaceable with procedural. So i can use multiple different functions in procedural scripts, and the functions arent specific to some things. But thats generally just for librarys, generally speaking OOP is the way to go for larger scripts imo.
Does anyone know how to instantiate an object on one php page and use it on another page?? Ex: main.php I verify the password and if it is correct i do: $currentUser = new user(); // a class then i have a frameset that goes to : main.php Here i want to do $currentUser->getUsername(); but I get an error Call to a member function getUsername() on a non-object Do I have to instantiate the object on every page? or is there some global scope i can use??
OOP is definitely the way to go if you want to build large applications. Even open source solutions like Joomla are now starting to implement their own frameworks. Some people who are "procedural only" use the fact that procedural code is faster. However in my opinion the positives far outweigh the negatives. With OOP adding and extending features is easy.
No, that won't work, the class needs be be included, typically, large applications will have one include file that includes everything that any page might need and construct globally used objects. If you don't construct the class using any posted parameters then that's your answer, else construct the class in the framed code. You do still need to declare the object as global inside any functions