
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
What are getters and setters methods in PHP?
In this article, we learn the best way to create getter and setter strategies in PHP. Getter and setter strategies are utilized when we need to restrict the direct access to the variables by end-users. Getters and setters are methods used to define or retrieve the values of variables, normally private ones.
Just as the name suggests, a getter method is a technique that gets or recovers the value of an object. Also, a setter method is a technique that sets the value of an object.
Example
Let's understand the use of getter and setter methods through an example.
<?php class Person{ private $name; public function setName($name){ $this->name = $name; } public function getName(){ return $this->name; } } $person = new Person(); echo $person->name; ?>
Output:
PHP Error Cannot access private property Person::$name
Explanation:
In our Person class above, we have a private property called $name. Because it is private property, we are unable to access them directly like the above and that will produce a fatal error.
Example
To run the code above and get our desired output Let's test this example.
<?php class Person{ private $name; public function setName($name){ $this->name = $name; } public function getName(){ return 'welocme'. $this->name; } } $person = new Person(); $person->setName('Alex'); $name = $person->getName(); echo $name; ?>
Output:
welcomeAlex
Explanation:
Here to give access to our private properties, we have made a "getter" function called getData, again because the visibility of the properties is set to private, you will likewise unable to change or modify their values. So, you should utilize one of the "setter" functions that we made: setName. After that, we have Instantiated our Person object.
We "set" the $name property to "Alex " utilizing our setter technique setData. Then We retrieved the value of the $name property by using our getter function getData.
- Related Articles
- What are Getters/Setters methods for Python Class?
- Getters and Setters in Kotlin
- Getters and setters in JavaScript classes?
- How to use getters and setters in TypeScript?
- PHP Static Properties and Methods
- What are the methods for sanitizing user inputs with PHP?\n
- PHP Magic Methods
- What are methods in Java?
- What are defender methods or virtual methods in Java?
- What are save and restore methods in HTML5 Canvas?
- What are native methods in Java and where are they used?
- What are Hierarchical Methods?
- what are abstract methods in Java?
- What are generic methods in Java?
- What are Event Methods in jQuery?
