Hiding Information in PHP 7

If you have already read the previous blog, you should have noticed that there was a hole in the previous code.
Did you notice that?
Let us examine this part of code:

$guiter = new Instrument('Elictric Guiter');
$guiter->setWeight(1);
$guiter->getWeight();
$guiter->weight = 10;
var_dump($guiter);

And the output is simply mind blowing.

object(Instrument)#1 (2) {
["name"]=>
string(15) "Elictric Guiter"
["weight"]=>
int(10)
}

Look, we set the weight of the instrument to less than 2 KGs – we’ve set it to 1 – but in the final output the weight has come up as 10.
How did it happen?
It’s happened, because, in the last line of code we have directly call the ‘weight’ property and assign a value which is greater than 2 KGs. It was simply pathetic. Our entire efforts of making ‘setter’ and ‘getter’ functions to restrict the weight less than 2 KGs just have vanished into blue. It was overwritten simply by one ‘access modifier’ – ‘public’.
If we had made it ‘private’ it could not have been accessed so easily.
Let us see what output comes out by changing the entire code.


name = $name;        
    }    
    public function getWeight() {
        return $this->weight;
    }    
    public function setWeight($weight) {
        if ($weight > 2){
            throw new Exception;
        }
        $this->weight = $weight;
    }    
}
$guiter = new Instrument('Elictric Guiter');
$guiter->setWeight(1);
$guiter->getWeight();
$guiter->weight = 10;
var_dump($guiter);

Here is the output.

PHP Fatal error:  Uncaught Error: Cannot access private property Instrument::$weight in /home/hagudu/Code/php7book1/Day2/instrument.php:23
Stack trace:
#0 {main}
  thrown in /home/hagudu/Code/php7book1/Day2/instrument.php on line 23

It’s worked finally. It cannot access the private property. It was what we wanted. One word – ‘private’ makes things worse for the unwanted assessors. 
Now imagine your home. Not every room should be accessible by the strangers who come to meet you. May be you invite that person to your sitting room. Your bed room is either protected or private.
Let us consider a class ‘Room’ and test our code.

name = $name;        
    }    
    public function getColor() {
        return $this->color;
    }    
    public function setColor($color) {
        if ($color == 'black'){
            throw new Exception;
        }
        $this->color = $color;
    }
    private function moveToBedroom() {
        return "Restricted";        
    }    
}
$home = new Home('Castle of the Heavens');
$home->setColor('white');
$home->getColor();
var_dump($home);

The output is quite expected.

object(Home)#1 (2) {
["name":"Home":private]=>
string(21) "Castle of the Heavens"
["color":"Home":private]=>
string(5) "white"
}

You have noticed that there is a private method. Now we’re going to access that method just by adding this line to our code (marked in red).

$home = new Home('Castle of the Heavens');
$home->setColor('white');
$home->getColor();
$home->moveToBedroom();
var_dump($home);

The output is utterly different from the previous one.

PHP Fatal error: Uncaught Error: Call to private method Home::moveToBedroom() from context '' in /home/hagudu/Code/php7book1/Day2/home.php:28
Stack trace:
#0 {main}
thrown in /home/hagudu/Code/php7book1/Day2/home.php on line 28

As we progress we’ll find more examples of such encapsulations. When you write your class, you must know what properties should be made public or what should be private. There is another ‘access modifier’, called ‘protected’. In the next chapter we’ll see how that works.

For further reading...

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s