What are getters and setters methods in PHP ?
In object-oriented programming, getters and setters are methods used to access and modify the private or protected properties of a class. These methods provide a controlled way to retrieve and update the values of class properties, promoting encapsulation and maintaining the integrity of an object’s state.
Getters in PHP
A getter method is responsible for retrieving the value of a private or protected property within a class. It allows external code to access the property’s value without directly manipulating it. Getters typically have names prefixed with “get.”
Example: Here, the getName method is a getter that returns the value of the private $name property.
PHP
<?php class Person { private $name ; public function __construct( $name ) { $this ->name = $name ; } public function getName() { return $this ->name; } } // Usage of the getter $person = new Person( "John Doe" ); echo $person ->getName(); ?> |
John Doe
In this example, .
Setters in PHP
A setter method is used to modify the value of a private or protected property within a class. It allows controlled access to the internal state of an object by providing a way to update its properties. Setters typically have names prefixed with “set.”
Example: Here, the setName method is a setter that updates the value of the private $name property.
PHP
<?php class Person { private $name ; public function setName( $newName ) { $this ->name = $newName ; } public function getName() { return $this ->name; } } // Usage of the setter and getter $person = new Person( "John Doe" ); $person ->setName( "Jane Doe" ); echo $person ->getName(); ?> |
Jane Doe
Benefits of using the Getters and Setters
- Encapsulation: Getters and setters encapsulate the internal state of an object, hiding its implementation details and providing controlled access.
- Validation: Setters can include validation logic to ensure that the new value meets certain criteria before updating the property.
- Flexibility: Getters and setters allow developers to modify the internal representation of a property without affecting external code that uses the class.
Differences between Getters and Setters
Getters |
Setters |
---|---|
Used to retrieve the value of a property. |
Used to modify the value of a property. |
Typically named with a “get” prefix (e.g., getName). |
Typically named with a “set” prefix (e.g., setName). |
Do not take parameters (or take optional parameters). |
Take parameters representing the new value to be set. |
Used for reading the state of an object. |
Used for modifying the state of an object. |
Conclusion
Getters and setters are crucial components of object-oriented programming in PHP. They provide a controlled interface for accessing and modifying the internal state of objects, promoting encapsulation and maintaining a clear separation between an object’s implementation details and its external usage. By using getters and setters, developers can enhance the robustness and maintainability of their code.
Contact Us