আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-7: Static Method, Properties and Late Static Binding

What is Static Method and Property?

It we want to access any method and property of a class without object then those methods and properties have to be declared as static. To declare static, we must use static keywords before Method and Property name. The static keyword is also called by access modifier. Let’s look at an example:

<?php
class testStatic{
  // static methods and properties are defined with the static keyword.
  //You can add static keyword before or after visibility (Public, Private, Protected)
  public static $count = 0;
  static public $counter=1; 
   
  public static function counter(){
       
      }
  static public function anotherCounter(){
       
      }
}
?>

How do we access or use Static Methods and Properties?

Static Methods and Properties uses can be divided into two different way:

  1. Inside of the class
  2. Outside of the class

Static Method and Property uses outside of the class:

If we want to use static methods and properties outside of the class then we have to put (::) scope resolution operator after the class name, then we have to put static method of property name. For better understanding, see the following example:

<?php
class testStatic{
     
  public static $count = 0;
 
   
  public static function counter(){
      echo "Its Static Counter";
  }
  
}
 
//Accessing Static Property and Method Outside of the Class
testStatic::$count=5; // Assigning Value to static Property
echo testStatic::$count; // Calling Static Property with Updated Value
echo "<br>";
testStatic::counter(); // Calling Static Method with Updated Value
?>

Note: After the scope resolution operator, the property name must be start with a $ (dollar sign).

Static Method and Property uses inside of the class:

It we want to use static methods and properties inside of the class without $this pseudo-variable, then we have to put the class name or self keyword before (::) scope resolution operator, then we have to put the method or property name. For better understanding, see the following example:

<?php
class testStatic{
     
  public static $count = 0;
 
   
  public static function counter(){
      echo "Its Static Counter: ";
      echo self::$count++,"<br>"; // accessing static Property inside class
  }
  
}
// Calling Static Method Outside Class
testStatic::counter(); 
testStatic::counter();
testStatic::counter();
?>

Result:

What is Late Static Binding?

In PHP, to call static methods and properties within the class, instead of using $this pseudo-variable, we use (::) scope resolution operator after the class name or “self” keyword. “Self” keyword only specifies the class in which it’s been used. And for that the methods and properties of the parent class can not be inherits by the child class. And for that from PHP 5.3 instead of using “self” keyword we have start using “static” keyword to inherit any the methods and properties from parent class to child class. And this is called Late Static Binding. To understand better, see the following examples:

Example: Use of “self” keyword

<?php
class Course{
    protected static $courseName = 'Professional PHP';
    public static function getCourseName(){
        return self::$courseName;
    }
}
  
class Student extends Course{
    protected static $courseName = 'Laravel';   
}
  
echo Student::getCourseName(); // Professional PHP, not Laravel

Explanation:

  • Firstly, We have created a class named Course, inside of this class there is a static property named $courseName and it’s value is “Professional PHP”. And then we also create a static method named getCourseName() which will return the value of $courseName property. Notice that, we have used “::” scope resolution Operator inside the Course class to access the static property.

  • Secondly, We have created a child class named Student from Parent Course class. And Student class also have a static property named $courseName which value is “Laravel”.

  • Thirdly, We have called getCourseName method from Parent Course class to Child Student class. So that, we get “Laravel” as course name for Student child class. However, the getCourseName method of the course class returns us “PHP Professional” as result instead of “Laravel”. The reason is that self keyword does not specify a child or any other class except the class that is called.

To solve the above problem, from PHP 5.3, Late static method has been added. Basically, Instead of “self” keyword, the “static” keyword is used, As it can points the class from which it’s been called. Let’s change our example above:

Example: Use of “static” keyword or Late Static Binding

<?php
class Course{
    protected static $courseName = 'Professional PHP';
    public static function getCourseName(){
        return static::$courseName;
    }
}
  
class Student extends Course{
    protected static $courseName = 'Laravel';   
}
  
echo Student::getCourseName(); // Laravel

Now we will get our desired result.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *