views:

416

answers:

6

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

+6  A: 

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.

Chacha102
Always using global in functions is a bad idea because it takes up a lot of space.But, would it be good to use $GLOBALS['class']? It might be longer to write but it would be good to use if you were only using the class once.
joshli
`$GLOBALS` is basically the same thing. You are creating a global variable.
Chacha102
So I can just use $GLOBALS['class']. Then on another line just use $class?
joshli
No. What I am saying is `$GLOBALS` is available anywhere, making it a global variable.
Chacha102
IMHO you should try to avoid globals. It can get very difficult to maintain such code.
Felix Kling
@joshli You don't want to pollute the global scope. Use a Registry if you have to have many objects globally available
Gordon
+4  A: 

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.

jboxer
+1  A: 

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

Sarfraz
A: 

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
+1  A: 

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
+2  A: 

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