Pages

Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Saturday, December 14, 2013

PHP-Reference Variable

Reference variable are those that have a reference to the value stored in them for example

 $name = 'fruit'; //Generic Varible  
 $$name='banana'; //Reference Variable  
 echo $fruit;   
 //Outputs Banana  


Here $name is a variable which stores name 'fruit' and in turn there is a memory location in your system when you run this program which is labelled as 'fruit' in which value is stored as $$name or $fruit.

Saturday, December 7, 2013

Static function,Variable accesibility

Recently one of my co-friends regarding  a programme which made my concepts clear for a static function, variable accessibility outside a class

 <?php  
 final class Hello  
 {  
      private static $greeting='Hello';  
      private static $initialized=false;  
      private static function initialize()  
      {  
           if(self::$initialized)  
           return;  
         self::$greeting='There';  
         self::$initialized=true;  
      }  
     public static function greet()  
     {  
       self::initialize();  
       echo self::$greeting;  
     }  
     public function hello1()  
     {  
       return 'hi';  
     }  
 }  
 echo Hello::hello1();  
 $obj=new Hello();  
 echo "==>";  
 echo $obj->greet();  
 ?>  


this outputs "hi==>There"

A static function though can be called with scope resolution operator via class name directly or from object.

A static variable can only be called from a static function or from a class name via scope resolution operator

And since we can call a static function directly we cannot use $this in the same

for example in static function greet we are calling the
echo self::$greeting;
and not
echo $this->$greeting;