19

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().

Birdman
  • 4,984
  • 11
  • 43
  • 65
ambiguousmouse
  • 1,945
  • 2
  • 22
  • 27
  • 21
    are microseconds of processing worth brain cells? open the source of something like CakePHP or CodeIgniter, then compare with source of wordpress, then tell me the latter doesn't encourage you smash your face into the keyboard. – seanmonstar Jan 23 '10 at 06:34
  • 5
    You are right. Life is too short to try and optimize for that 50 milliseconds of optimizations per request. – ambiguousmouse Jan 23 '10 at 06:42
  • @seanmonstar: You mean Wordpress is not OO?! **o_O'** – Alix Axel Jan 23 '10 at 06:49
  • You might also be interested in this question http://stackoverflow.com/questions/716412/why-use-php-oop-over-basic-functions-and-when – Felix Kling Jan 23 '10 at 06:50
  • 2
    Using OOP in PHP is slower? Slower than what? Got any hard facts to prove that statement or is it just something you picked up from numerous silly comments coming from inexperienced "programmers"? – N.B. Jan 11 '12 at 11:49
  • OOP is slower in my environment, granted not by much. Every ms counts in my program so I use procedural. Procedural is not that difficult to manage if you know what you're doing. – Hobbes Nov 19 '15 at 20:54
  • Just dont do MVC that crap is hell (see the horrors of CI) – nodws Jun 26 '17 at 19:19

9 Answers9

12

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.

jakeboxer
  • 3,210
  • 4
  • 25
  • 27
  • 1
    PHP7 is one of the fastest web languages there are. (its faster than python) – Erik Thiart Jan 09 '19 at 08:41
  • back in the day PHP was slow, there is even someone saying in one answer that java kicks PHP's ass. That might have been true in 2010-2012 when these answers were written. PHP 7.x really is fast. And Java has since fallen out of favor because it is slow. On servers with multiple PHP versions, switching from php5 to php7 will sometimes double the speed of the page processing. Even going from 7.0 to 7.3 results in about 10% speed increase, so yes, things have changed in the meantime. – brett Sep 09 '19 at 15:43
10

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.

Tyler Carter
  • 59,289
  • 20
  • 126
  • 148
6

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.

Austen Hoogen
  • 61
  • 1
  • 2
6

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.

symcbean
  • 46,644
  • 6
  • 56
  • 89
5

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.

Dominic Barnes
  • 27,429
  • 8
  • 63
  • 89
4

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

Hobbes
  • 775
  • 9
  • 28
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.

Rikaelus
  • 535
  • 5
  • 14
3

OOP has more merits than its de-merits. See PHP OOP, What Are The Benefits?. Also see for OOP vs PP in PHP.

redburn
  • 545
  • 4
  • 24
Sarfraz
  • 367,681
  • 72
  • 526
  • 573
2

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).. :)

Chris
  • 7,287
  • 8
  • 33
  • 50
  • Huh. why does not using OOP imply copy+paste-coding? – Sebastian Mach Sep 19 '11 at 08:40
  • procedural programming is not reusable. although you can make this mistake in OOP too :) – Chris Oct 17 '11 at 15:26
  • 1
    Why not reusable? You can write very, very generic, procedural code. See the C++ standard library (e.g. `std::sort`), see the C standard library (most prominently `qsort`). Procedural code not being reusable would totally imply that you can use every function once and once only, which of course is a bit non-well. – Sebastian Mach Nov 02 '11 at 09:40
  • Ok it is possible to write good functional code the reality is that there are a lot of legacy spaghetti projects in php which use procedural code whereas most newer php projects basically are all oop and are usually structured better. – Chris Oct 08 '14 at 10:44
  • 1
    Likewise, you can write nonreusable OOP code. It is not really relevant to the question what the reality is, however. – Sebastian Mach Oct 08 '14 at 13:36