PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6) by Chan Jamie & Publishing LCF

PHP: Learn PHP in One Day and Learn It Well. PHP for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project Book 6) by Chan Jamie & Publishing LCF

Author:Chan, Jamie & Publishing, LCF [Chan, Jamie]
Language: eng
Format: epub, azw3, pdf
Publisher: LCF Publishing
Published: 2020-06-18T16:00:00+00:00


9.6 Getter and Setter

In the previous section, we talked about the difference between public and private class members.

Whenever possible, we should declare class members as private if code outside the class does not need to access them. This act of preventing code outside from accessing class members unnecessarily is known as encapsulation.

Encapsulation makes it easy for us to make changes to class members without affecting code outside the class. For instance, if we want to change the name of the $id property in our Movie class to $movieID , we only need to make changes within the Movie class, any code outside the class is not affected. This is one of the advantages of declaring a class property as private .

Another advantage of declaring class properties as private is that it helps prevent unauthorized modifications to our object properties. To see why this is so, add the following code to chap9.php :

$mov1->rentalPrice = -20;

echo $mov1->rentalPrice.'<BR>';

Here, we first assign -20 to the $rentalPrice property of $mov1 . Next, we use an echo statement to echo its value. If you run the code above, you’ll get -20 as the output.

As you can see, we manage to change the $rentalPrice property of $mov1 to -20 . This is because $rentalPrice is a public property. Hence, we can access it outside the Movie class and change it to any value we like. This is definitely not desirable as rental price should not be negative.

To prevent such modifications from happening, we should not declare the $rentalPrice property as public . Instead, we should declare it as private . Try changing the $rentalPrice property to private in Movie.php and run chap9.php again, what do you get?

You get something similar to the output below, right?

Fatal error: Uncaught Error: Cannot access private property Movie::$rentalPrice in…

We are no longer allowed to access and modify the $rentalPrice property of $mov1 as it is now a private property.

Whenever possible, we should always declare our class properties as private ; this helps to prevent any unauthorized access or modifications to them. However, if we declare all our class properties as private , what happens if code outside the class needs to access or modify those properties?

In cases like these, we can use getters and setters. These are magic methods that allow us to provide limited and controlled access to our private and protected properties.

To see how this works, add the following methods to Movie.php (after the displayHeading() method but before the closing brace of the Movie class):

public function __get($propertyRequested){

if ($propertyRequested == 'id')

return 'You do not have permission to access id.<BR>';

else

return $this->$propertyRequested;

}

public function __set($propertyToModify, $value){

if ($propertyToModify == 'rentalPrice' && $value > $this->rentalPrice)

$this->rentalPrice = $value;

else

echo 'Failed to modify '.$propertyToModify.'<BR>';

}

The first method ( __get() ) is known as a getter; it controls which property can be accessed outside the class and has one parameter called $propertyRequested . This parameter stores the name of the property we want to access.

Within the __get() method, we use an if-else statement to check If $propertyRequested equals 'id' . If



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.