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
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.azw3
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.pdf
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.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7811)
Grails in Action by Glen Smith Peter Ledbrook(7722)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6875)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6871)
Running Windows Containers on AWS by Marcio Morales(6403)
Kotlin in Action by Dmitry Jemerov(5092)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(5089)
Combating Crime on the Dark Web by Nearchos Nearchou(4664)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4655)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4438)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4355)
The Age of Surveillance Capitalism by Shoshana Zuboff(3989)
Python for Security and Networking - Third Edition by José Manuel Ortega(3907)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3573)
Learn Wireshark by Lisa Bock(3567)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3530)
Mastering Python for Networking and Security by José Manuel Ortega(3378)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3359)
Blockchain Basics by Daniel Drescher(3330)
