Sunday, June 05, 2011

Visibility in PHP 5.3

Visibility is very important aspect of Object Oriented Programming (OOP), one of three basic ideas. It is also called encapsulation. Using key words like public, protected and private we can restrict access to properties and methods. It is not necessary to write always keyword public or protected or private BUT it is a good habit to do it. When method or property doesn't have visibility keyword it means it is public.

How it works?
Let start with the key word public. In real life when something is public it means that we can use it without permissions from anyone. We also don't need to tell anyone that we want to use it. For example in Amsterdam (capital of Holland - county in Europe) it is very popular to use public bicycles. These bicycles are public (and free). You can take one and drive from one point of city to another. You don't need to ask anyone for permission. In OOP we have a very similar situation. We can say that property or method is public. It means that there is no restriction to use it. We can use it everywhere.

Example:

class Holland
{
    public method borrowBicycle()
    {
        $bicycle = new Bicycle(); // assume we have also class Bicycle defined somewhere earlier
        return $bicycle;
    }
}

Absolutely no restrictions on method borrowBicycle(). Everyone can borrow a bicycle.

So please remember: PUBLIC = NO RESTRICTIONS

Now, let move to completely opposite key word called private. It means the same like in real life too. Suppose I have a computer and it is my computer. Mine and only mine. I don't wish to allow other person to use it without my permission. It doesn't matter who you are. Even Osama bin Laden can't use my computer without my permission. It is available only for me. Of course I can allow other people to use it. See example:

class Lucas
{
    private $_computer; // assume that $_computer value is "PC INTEL PENTIUM 3"

    public method borrowComputer($who)
    {
        if ($who == "Osama bin Laden") {
            return $_computer;
        } else {
            return null;
        }
    }
}

In example above. Everybody can ask to borrow (my) computer. borrowComputer() is public method. You can ask, my sister can ask, any one can ask. But I always will answer "no" except "Osama bin Laden" will ask to borrow my PC (guess why :-D). Remember: only class where is defined private property can use it without permission and only inside that class we can allow other objects to use our private property. PRIVATE IS THE MOST RESTRICTIVE.

One more example about private. This time I have private method _prepareTest(). To show it better I will also add public method doTest().

class Lucas
{
    private method _prepareTest()
    {
        $test = new Test(); // assume we have also class Test defined somewhere earlier
        return $test->__toString(); // we will talk about magic method __toString today, you only need to know that it return String value
    }

    public method doTest()
    {
         $now = date(); // $now means current time
         if ($now < "20.06.2011) {
             return "We have time. Will do later";
         } else {
             return $this->_prepareTest();
         }
    }
}

Code above has public method doTest(). Any one can order me doTest() but if current date is earlier then 20th June then I reply "We have time. Will do later". When it is later then 20th June then I really do test using my private method _prepareTest(). Notice no one can order my "prepareTest". No one know how I prepare test. It is my private method.

The last key word is protected. It is something between public and private but closer to private then public. Think about protected like about private but not for objects which extends that class. So, how protected work in real life? Assume my father has a car. I have driving license and my father allows me to borrow a car without asking him. It is like my own but for example my friend (who isn't my brother) can't borrow my father car without permission. My friend not inherit from my father. They even don't know each other.

Example:

class Father
{
    protected $_car; // assume $_car value is "PORSHE CARRERA 911"

    public borrowCar()
    {
         return "NO, I WONT BORROW YOU MY CAR"; // any one can ask my father to borrow his car, but the answer always is "NO"
    }
}

class Lucas extends Father
{
     public function __construct()
     {
         echo 'I have a car: ' . $this->_car; // I am inherit from my father, his protected car is available for me without asking
     }
}

Code above is really self descriptive. Any one can ask my father to borrow him car and answer is always "No". But I am son of my father and my father allows his sons to use his car without permission, so I do :-) REMEBER: PROTECTED IS LIKE PRIVATE BUT NOT FOR CLASSES THAT INHERITS FROM CLASS WHERE PROTECTED IS DEFINED.

1 comment:

  1. Pengen yang lebih seru ...
    Ayo kunjungi www.asianbet77.com
    Buktikan sendiri ..

    Real Play = Real Money

    - Bonus Promo Red Card pertandingan manapun .
    - Bonus Mixparlay .
    - Bonus Tangkasnet setiap hari .
    - New Produk Sabung Ayam ( minimal bet sangat ringan ) .
    - Referal 5 + 1 % ( seumur hidup ) .
    - Cash Back up to 10 % .
    - Bonus Royalty Rewards setiap bulan .

    Untuk Informasi lebih jelasnya silahkan hubungi CS kami :
    - YM : op1_asianbet77@yahoo.com
    - EMAIL : melasian77cs@gmail.com
    - WHATSAPP : +63 905 213 7234
    - WECHAT : asianbet_77
    - SMS CENTER : +63 905 209 8162
    - PIN BB : 2B4BB06A / 28339A41

    Salam Admin ,
    asianbet77.com

    Download Disini

    ReplyDelete