PHP - Setters and getters
Article Metadata
Contents |
Introduction
In this article we will shortly go thru PHP's concept of setters and getters. This is common method to make private class variables accessible outside the class and anywhere from the rest of the program.
To make this happen, we need to create two "utility" functions called a setter and a getter, within the class itself. The purpose of the setter is to set the value of the private variable whereas the getter will release the value outside the class.
Prerequisites
PHP enabled on a web server (You can dowanload WAMP (Windows Apache Mysql PHP) for windows and LAMP for linux)
Web browser of your choise
Text editor of your choise (You can use notepad++)
Implementation
We will create a very simple page, which will contain both HTML and PHP embedded. What it does is only that it sets and gets the private function value and echoes the result on screen for demonstration purposes.
Example code
To make this happen, we will need to first create a new object called "playerObject". This object further accesses the "Users" class for its private variable.The example code lines are commented below, step by step.
On the first time, running the getter function for the class will return the variable but it is empty. so nothing will be echoed. After that the setter function will be used and the variable will be set to contain a value.
The second echo attempt then will display the content of the variable, this was achieved by the getter function. We will repeat this a couple of times for demo purposes.
01 <?php
02 class Users{
03 private $userName;
04 public function setUserName($x) {
05 $this -> userName = $x;
{
06 public function getUserName() {
07 return $this-> userName;
}
08 ?>
09 <html>
10 <head><title>PHP - Setters and Getters</title></head>
11 <body>
12 <?php
13 $playerNameObject = new Player();
14 echo $playerNameObject->getUserName();
15 $playerNameObject -> setUserName("Michelle");
16 echo $playerNameObject->getUserName();
17 $playerNameObject -> setUserName("Lena");
18 echo $playerNameObject->getUserName();
19 ?>
20 </body>
21 </html>
Code explanation
| Line number | Description |
|---|---|
| 01 | Start PHP code section (You can use <?php or <? to start php section. The <? will work only if the "short open tag" is enabled in php.ini file. |
| 02 | Start the Users class declaration. |
| 03 | Create class private function. This is not accessible from outside.To make this working, we need to create two utility functions; a setter and a getter. Setter takes a private variable and allow to change them. Getter functions take private variable and retrieve it. |
| 04 | Create a public function to be usable from anywhere the code. The code convention standard is to write these functions starting with a small letter and then using capital letter for the rest of the words included. |
| 05 | We will pass in variable "$x" and will set the variable content equal to that name. |
| 06 | Define the getter function as public |
| 07 | Define the return value to be the contents of the private variable "userName" |
| 08 | Close the "PHP" code section |
| 09 | Start the "HTML" code section |
| 10 | Set the title of the html page |
| 11 | Start the "<body>" section of the code |
| 12 | Start the "PHP" code section |
| 13 | Create new object "playerObject"based on "userObject |
| 14 | Use the getUserName public function to display the content of the private $userName. However, at first echo this will display empty, since we have not yet set any content for this private variable. |
| 15 | Here we use the setter function to put the value "Michelle" into the class private variable. |
| 16 | Because we had used the setter, now the echo will display the content of the variable, thus we will see "Michelle" onscreen. |
| 17 | We further change the variable name with the setter function, this time to "Lena". |
| 18 | The getter function is called and echo will display "Lena" |
| 19 | Close "PHP" code section |
| 20 | Close "<body>" section |
| 21 | Close the "HTML" code section |
Tested with
Mozilla Firefox 5.0
PHP 5.x


Hamishwillee - Why is this in "Ovi" category?
Its not clear to me why this is on the Nokia Developer website? What specific mobile problem is this designed to fix. Why is it in the Ovi category?hamishwillee 06:00, 12 October 2011 (EEST)