Tuesday, 21 July 2015

PHP Variable Varibles

In PHP, it supports to store name of a variable into another variable and there by these variable names can be changed dynamically. This concepts is known as variable variables which we have listed as one of the special features of PHP.

Accessing Variables

Variable Variables are start with double dolor($$) to access the value of the variable, since it is a tree like access like {variable variables name} -> {variable name} -> {variable value}. For example,
$language = "PHP";
$category = "language";
echo $$category;
While running this program, first it will find the value for $category as language as initialized. Then, this value will be joined with the remaining dolor($) sign to find the value of $language to be displayed to the browser using the PHP printing statementfinally.

Accessing Class Properties

We can access the class properties using this variable variables from outside the class. For that, the name of the class property should be stored into a variable being withglobal scope for accessing it even from outside the class. For example,
class College {
var $name = "Arts and Science international";
var $category = "Arts";
}
$objCollege = new College();
$collegeName = "name";
echo $objCollege->$collegeName . "<br/>";
Now the above program will display the value of the name property of the class. Generally, the class properties are accessed by its name without specifying any $ sign. For example, the equivalent code for the same behavior without using any variable variables is as shown below.
//Usual way of accessing class properties
....
echo $objCollege->name . "<br/>";
But, for showing PHP variable variables behavior, we store the name of the class property into a variable $collegeName and there by access the property value by using this variables.
php_variable_variables

Accessing Array Values

Variable variables concept is quite interesting while working with arrays. Why because, since we are using two variables for specifying the name and value separately, there will be more than one possible interpretations in finding values of array elements. Let us, look into this point in detail with an example.
$movies = array("Django","Life of Pi");
$entertainment = "movies";
echo $$entertainment[0];
By default, the interpretation will be done by ${$entertainment[0]}->$m, and since there is no variable $m is defined, an error notice will be returned to the browser, like,
Notice: Undefined variable: m in ..\variable_variables.php on line 4
To resolve this error, we need to compute the value of $entertainment instead of$entertainment[0], by specifying curly braces around it. For example,
...
echo ${$entertainment}[0]; // output: Django
By changing the echo statement of previous program with the specification of program interpretation, it will return the other possible result to the browser.
Similarly, while accessing the class properties which are stored as array values, the variable variables require specification as per the expected result of the developer. For example,
class College {
var $name = "Arts and Science international";
var $category = "Arts";
var $u = "Uncategorized";
var $ugCourses = array("B.A","B.Com","B.Sc");
}
$objCollege = new College();
$courses = "ugCourses";
//Output: Uncategorized
echo $objCollege->$courses[0] . "<br/>";
//Output: B.A
echo $objCollege->{$courses}[0] . "<br/>";
If we use this concept unnecessarily that will increase number of code. Rather, it will be useful if we utilize this property in optimistic manor to reduce line of code.

0 comments:

Post a Comment