5 Reasons Why Use Object Oriented Programming (OOP) in PHP

Hi guys! I’d like to share to you on why use OOP in PHP. Btw, this is my first post here, so I’m really excited! *giggles*

First of all why do we have to use OOP?

We all know that there are two types of programming languages, the Procedural Type, and the Object Oriented. Procedural Types are common in C languages and ASMs, it may also be the first type of PL you may have used. And there is OOP or Object Oriented Programming. Vb is an example of an OOP type.
I don’t want to elaborate those things because I am assuming that you guys already know the difference. If you don’t, you might want to do some Google-ing stuffs.

So back to PHP. We’ve seen PHP as a procedural language. But obviously, it is not. PHP can also do some OOP stuff too.

So why the heck do we need to use OOPs?

Let me tell you something, if you think you don’t need these stuff, you might as well reconsider yourself as a low tech programmer. Why? Because a lot of developers avoid procedural ways.  And there are lots of reasons why not to use procedural.

Personally, these are the reasons why we should use OOPs:

  1. Its flexibility – OOPs are really flexible in terms of using implementations.
  2. It can reduce your source codes by more than 99.9% – it may sound like I’m over exaggerating, but it is true. Let me give you an example:

Without OOP:

$planetWorld = "Hello world!";
 
$planetMars = "Hello Mars!";
 
$planetVenus = "Hello Venus!";
 
$planetJupiter = "Hello Jupiter!";
echo $plantMars;
 
echo $plantWorld;
 
echo $plantVenus;
 
echo $plantJupiter;

With OPP

This is myclass.php:

class Planet {
 
public $Venus;
 
public $Mars;
 
public $World;
 
public $Jupiter;
public function __construct() {}// so it can be used anytime: also allows you to create a new object
$this->World = "Hello world!";
 
$this->Mars = "Hello Mars!";
 
$this->Venus = "Hello Venus!";
 
$this->Jupiter = "Hello Jupiter!";
}

Sample php file:

include(“myclass.php”); //assuming that you have already declared the variables here in a class.
$myPlanets = new Planet();
 
echo $myPlanets->Jupiter;
 
echo $myPlanets->World;

but there are more lines, I thought it could make my code shorter? – you may not see the great advantage of it in just one page, but what if you need the variables in another page or more? Are you going to declare the same value all over again?

It doesn’t matter! I can copy paste it easily! Haha! – perhaps, but what if the Planet Mars had to be renamed? Are you going to change all the pages one by one again? Where in OOP, you can just change it to the class easily.

You see, in programming, code repetitions are strictly not advisable as a coding practice. You should avoid code repetitions as much as possible.  

Most of the programmers today still use primitive variables. Primitive variables are like this:

Int x = 0;
String y = “abc”;

Professional programmers  are avoiding these types of variables now. Because it is really  not really a good practice. Like what I’ve said, repetitions are strictly avoided as much as possible.  As for this example:

This is myclass.php:

class Planet {
 
public $World;
public function __construct() {}// so it can be used anytime: also allows you to create a new object
$this->World = "Hello world!";
Public function setWorld($val)
 
{
 
$this->World = $val;
 
}
 
}

And for the sample php file:

include(“myclass.php”);
 
$newPlanet = new Planet();
 
$newPlanet->setWorld(“Hello EARTH!);
 
echo $newPlanet->World;

bah! Its faster to declare it in a primitive variable so I can do this easily $Earth = “Hello EARTH!” – It may look like it is, but what if the input comes from a user? How are you going to do the data filtering? And this is the 3rd reason why:

3. It’s much easier in implementing security – We all know that security is one of the vital requirements when it comes to web development.  Using OOP can ease the security implementations in your web projects. Example:

This is myclass.php:

class Planet {
 
public $World;
public function __construct() {}// so it can be used anytime: it also allows you to create a new object
$this->World = "Hello world!";
Public function setWorld($val)
 
{
 
If($val  != NULL) $this->World = $val;
 
Else{die(“Earth must contain a string”);}
 
}
 
}

For the sample:

include(“myclass.php”);
 
$newPlanet = new Planet();
 
$newPlanet->setWorld($_GET[“Planet”]);
 
echo $newPlanet->World;

But I can do this instead: if($_GET[“Planet”] != Null)  $newPlanet->setWorld($_GET[“Planet”]); – Like what I have said, we should avoid code repetitions, are you going to do this again and again where you can just declare these in a class?

I still don’t get you – Ok; let’s just say that the input must not contain special characters or numbers. How are you going to filter it? Makes sense eh?

4. It makes the coding more organized – We all know that a Clean Program is a Clean Coding. Using OOP instead of procedural makes things more organized and systematized (obviously).

5. It helps your team to work with each other easily – I know some of you had/have experienced team projects and some of you guys know that it’s important to have the same method, implementations, algorithm etc etc etc. Using OOP can help you work like your brains are in a LAN connection. For example, declaring variables are one of the harderst thing, so what if you declared this one $planetWorld while your teammate declared $worldPlanet? Isn’t it confusing? Unorganized? Thus, unorganized and not systematic programming will slow down your production development.

Cool! Can you teach me how to do it? – I’m going to post another article in which I will write a tutorial. So stay tuned for that. icon razz 5 Reasons Why Use Object Oriented Programming (OOP) in PHP or if you’re too excited, Google is your friend icon biggrin 5 Reasons Why Use Object Oriented Programming (OOP) in PHP

if you think I’ve missed one important point(s), let me know by hitting the comment box. Thank you! icon biggrin 5 Reasons Why Use Object Oriented Programming (OOP) in PHP

  • zplits

    iba na talaga…

  • Bluemoon_078

    imba. really nice article. :)

  • MooGeek

    guys the -> means -> we’ll fix this bug soon. :D

    • http://www.webninjawannabe.com Ven Francis

      Fixed…. :)

  • MooGeek

    Oopps its -& g t ; without the spaces

  • http://www.webninjawannabe.com Ven Francis

    thanks for the interesting topic bro. we’re hoping for more! :)

  • Ary4

    nice…

  • Ary4

    nice…

  • Aliansafeer

    its nice

  • dee

    Nice post

  • Asdasd

    Nice post.When will be ur next post?

  • PINKpearl Fernandez

    ahhh ..,thats why..

  • Dan from England

    Thanks mate, I was scratching my head about this one. It’s all clear to me now!

  • http://profile.yahoo.com/ZQHHVQHZPQQXWOLW5YHG577XWA prasanth p

    Good and useful article

  • Tony

    ahahah very funny! with OP you reduce your code by 99.9% but at end your comparation example was much longer in OOP.

    LOL you’re very funny guy!

    OOP is not a sience, is a way to think. You can done a perfect code with and without OOP.

    This is the only truth!

  • Tony

    you can do same stuff without OOP look this:
    in a file named: common.php we write this:
    $planetWorld = “Hello world!”;
    $planetMars = “Hello Mars!”;
    $planetVenus = “Hello Venus!”;
    $planetJupiter = “Hello Jupiter!”;

    then in the main file and all others just:
    include “common.php”;
    echo $plantMars;
    echo $plantWorld;
    echo $plantVenus;
    echo $plantJupiter;

    —————————————————————-
    as you can see no OOP is shorter than OOP.
    Conclusion: OOP is a religion, and not the only way to do….Choose your religion (the best paid IRL) and go on.