Is OOP worth using in PHP?
There are many debates on whether Object Oriented Programming is good or not开发者_运维问答. But, using OOP in Php is slower. Would it be a good trade to use procedural programming and faster speed and OOP with slower speed (since classes have to be initiated every time a page loads and big websites will start to become slow).
More importantly, would it be good to wrap stuff inside a class and use static functions or would it be better to just have many lying functions with a prefix ex: wp_function().
If the reason you're worried about using OO with PHP is speed, fear not: PHP is a slow language all around. If you're doing something that's processor-intensive enough for the speed loss from using objects to matter, you shouldn't be using PHP at all.
With regards to static functions, this is a design choice, but I'd err on the side of avoiding classes made up entirely of static functions. There's really no advantage to it over prefixes, and using a construct just because it's there isn't a good idea.
Yes, it is almost always a good idea to use OOP. This is because OOP is a style of coding, and coding styles for the most part are easily able to be transferred accross languages.
People don't use coding-styles because they use a certain language. People use coding styles because the style of coding offers good methods to do things they feel are desirable. Therefore, as long as the basic elements are there (inheritance, class properties, etc), it will always be viable to write in that coding style.
No, using procedural functions to access them probably isn't a good idea. This is because, you probably will have to do something like this to maintain the state.
function myFunc()
{
global $class;
$class->doMethod();
}
function myFunc2()
{
global $class;
$class->doMethod2();
}
This is a bad idea as it creates a ton of global state.
I strongly disagree with Chacha102's answer.
A proper answer to this question would fill several books - never mind a 20 line post here.
Both approaches have their benefits and drawbacks. I would recommend anyone who wants to consider themselves a good programmer to have significant experience in procedural, non-procedural and object-oriented programming. As well as experience with different methodologies such as SCRUM, cascade and RAD.
Regarding PHPs suitability for OO vs procedural coding, certainly the roots of the language are in the latter (but note that both Java and ASP are hybrid rather than true OO languages).
Peronally, I tend to write procedural code when I need to produce something which is either very simple or must have its behaviour to be thouroughly defined and predictable. However when writing complex code where the behaviour will vary greatly at run-time, I find OO to be vastly more efficient in terms of developer time - despite the design being based around a finite set of use-cases.
To argue that you should always write procedural code because it will run faster than OO code:
1) is not necessarily true 2) totally ignores the relative cost of developer time vs hardware costs
would it be good to wrap stuff inside a class and use static functions
Given that namespaces are now available in PHP, this is a really messy way to avoid namespace collisions and not something I would recommend.
C.
The same arguments about performance were made about Objective C and C++ back in the day. And the answer to that problem was to take advantage of available memory and processing power that is continuously getting bigger, better and faster.
Yes, OO requires more resources to run. But the benefits of using OO outweigh the hardware cost $$ (which is likely to be insignificant) of supporting OO applications.
It is, however, a good thing to be concerned about software performance. However looking under the hood of procedural vs. oo as a place to start is a bit misguided. You need to be focused on writing efficient code to begin with, whether procedural or OO (and both are relevant).
Keep in mind that even though PHP may not be the fastest platform out there (Java, for instance, kicks its butt) PHP is used to power some of the most traffic heavy websites on the Internet: namely Facebook.
If you have any other doubts about PHP and OO, just look at Zend and Magento (based on Zend). Magento is a VERY resource-intensive platform, memory usage can be upwards of 36MB per instance. However the platform itself is capable of handling millions of hits. This is because a properly configured server environment with a healthy serving of hardware resources make all the benefits of using OO far outshine the cost of the server itself. But in a world of clustered computers, NOT using the processing power and memory (responsibly) available to you is--IMHO--clinical insanity.
In my humble opinion, PHP developers should not try to go solely one direction. (procedural vs object-oriented) In some cases, all you need is a few global functions, other times it is more beneficial to use objects. Don't try to force everything one way or the other, be flexible and use what works best for each situation.
I was curious of this myself. Unfortunately after I changed my code from procedural to oop I ran some benchmarks and not beforehand.
Here's the benchmark code.
class game{
function maxp($val){
return max(0,pow($val,0.5));
}
}
$game = new game;
for($i=0;$i<100000;$i++){
$game->maxp(100);
//game::maxp(100);
}
OOP results ranged between 0.13 and 0.2 seconds;
Procedural results ranged between 0.08 and 0.1 seconds.
The results remained consistent over a good length of time.
I encourage you to run your own tests.
php 5.4.3
There's really no perfect answer since it depends on so many unknown variables, and then it doesn't have to be all-or-nothing.
For example, if you split your application into the MVC model, you might have your Model be OO but keep the Controller more simplistically procedural.
You could use classes as a means to simply group common static functions, or you could take it a lot farther into the active record pattern.
If you're building a small single-page webform that shoots a POST off in an email, you really don't need OO--lest you perhaps include an existing mail class to leverage.
Nobody can give you proper advice without understanding the project you're taking on.
That said, if your only concern is speed, then OO will be slightly slower. And there's a lot of sneaky things you can do in even procedural PHP to mimic some of the OO gains. But unless you're taking on a huge project, the added overhead will never amount to much. And by the time you have a huge project, the pros of OO might outweigh the cons of its overhead.
OOP has more merits than its de-merits. See PHP OOP, What Are The Benefits?. Also see for OOP vs PP in PHP.
Yes as your application grows.. (and it will) it will save you many hours of frustration. And repeating yourself (copying pasting code all over the place).. :)
精彩评论