Surfe.be - passive income

How to Use Static Properties in PHP ?

Static Methods and Static Properties

Sometimes, it is useful if we can access methods and properties in the context of a class rather than an object. To do this, you can use static keyword. So we can access static method using class name along with scope resolution .

How to declare static method or static property ?

class User 
{
  public static $your_variable_name; // You can use any access specifier 
	
  public static function your_method_name()
    {
	//code goes here
    }
}

Accessing non-static method

class User 
{
	public static $name = 'john'; // You can use any access specifier 
	
	 public function display()
	{
		return self::$name; // use self keyword to access static property
	}
}

$user = new User();
echo $user->display(); // Look our method was non-static . so we can access using User class object

Look here we use self:: to access static property inside class method .. Hope you will understand

Accessing static method

class User 
{
  public static $name = 'john';
	
    public static function display()
     {
	return self::$name;
     }
}

echo User::display();

Look here we declared a static method in our User class . So if we declare a static method then we can access there using just class name . No need to create object to access there . Hope you will understand all the procedure .

Note : This is actually the main difference with a normal method : $this is not available in such methods.

Static variables

Just like there are static methods and static functions, there are both static properties AND static variables. Static variables are variables that stay within a context, and are available next call.

function hello() {
    static $i = 0;
    echo $i++;
}
hello();
hello();
hello();
hello();
// 0123 

This actually works like some prehistoric class : the static variable is available to store some cached data : here, we have used it as counter, but static variables makes great cache for any dictionary that shouldn’t be loaded each call. Load the dictionary at the first call, then it will reside in memory for the rest of the execuction.

Note that the variable is completely invisible from the outside world, just like a private property.

See a little bit harder example

class Foo
{
  static public function staticMethod() { 
    return "I am a example of Foo class<br>";
  }
}

class Bar
{
  static public function staticMethod() {
      return "I am a example of Bar class";
   }
}

function doSomething($obj)
{
  // What type is $obj? We don't care.
  echo $obj->staticMethod();
}

doSomething(new Foo());
doSomething(new Bar());

OUTPUT:

I am a example of Foo class
I am a example of Bar class

So by allowing access to static methods via the object instance, you can more easily call a static function of the same name across different types.

Review your code with static analysis

  1. 1 . static method : avoid using $this in those methods, and consider adding static to methods that does’nt use this variable. .
  2. 2 . We use self keyword to access static property in a class .
  3. 3 . We use class name to access static method .

 

If you like this tutorial then please share with yours friends and if you find any error of this code  then please share with me .

Leave a comments

LET'S SOCIALITE

Recent Tweets

RECOMMENDED POST