Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 463
ambiguousmouse Avatar asked Jan 23 '10 06:01

ambiguousmouse


2 Answers

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.

like image 197
jakeboxer Avatar answered Sep 30 '22 19:09

jakeboxer


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.

like image 20
Tyler Carter Avatar answered Sep 30 '22 18:09

Tyler Carter