আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী 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-10: PHP OOP Overloading

What is Overloading?

From outside of the class, according to class user demand, automatically creating any property or method (Which has not been define before) in class is called Overloading. We can also tell that, Overloading is a process of how to automatically create a property and method according to class user demand.

Types of Overloading

Overloading is basically two types:

  1. Property Overloading
  2. Method Overloading

What is Property Overloading?

From outside of the class, according to class user demand, automatically creating any property (Which has not been define before) in class is called Property Overloading. We can also call Property Overloading as a way to automatically create property according to class user demand.

At property overloading, we set the value to the overloaded property with the __set() function, Get the value of Overloaded property with the __get() function, With the __unset() function, we set the value of the overloaded property to empty from the outside of the class and check the value of the Overloaded property with the __isset() function from outside of the class.

What is the job of __set() and __get() functions for Overload property?

The __set() function is used to create a new property from the outside of the class. And value can be set on the property created. The value of the overloaded property with the __get() function can be obtained from the outside of the class.

<?php
class PropertyTest
{
    /**  Location for overloaded data.  */
    private $data = array();
 
    public function __set($name, $value)
    {
        echo "Setting '$name' Property to '$value'\n";
        $this->data[$name] = $value;
    }
 
    public function __get($name)
    {
        echo "Getting '$name' Property with Value : ";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }
 
}
 
 
$obj = new PropertyTest;
 
$obj->a = "Masud Alam";
echo $obj->a . "\n";
 
?>

Result:

Explanation: First we create a property $a with __set() function (which was not defined in our class),also we set a value for $a. At the same time, keep a property name and its value in a private array property named $data. Then, with the __get() function, we get the value of the $a property from $data array property.

What is the act of the __isset() function to overload the property?

When an overloaded property is checked with the isset() function from the outside of class, Then the __isset () function is automatically executes in class. And allows you to check whether there is a value of overloaded property.

<?php
class PropertyTest
{
    /**  Location for overloaded data.  */
    private $data = array();
 
    public function __set($name, $value)
    {
        echo "Setting '$name' Property to '$value'\n";
        $this->data[$name] = $value;
    }
 
    public function __get($name)
    {
        echo "Getting '$name' Property with Value : ";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }
    // Checking Property Value is set?
    public function __isset($name)
    {
        echo "Is '$name' Property set?\n";
        echo isset($this->data[$name])?"Yes '$name' Property is set":"No '$name' Property is Not set";
        echo "\n";
    }
 
}
 
 
$obj = new PropertyTest;
 
$obj->a = "Masud Alam";
echo $obj->a . "\n";
isset($obj->a);
isset($obj->b);
 
?>

Explanation: Notice, at line number 35 and 36 with function isset(), we have checked that is there any value in the two unknown property in the class, And with the isset () function call, the __isset () function is automatically executed on line 21 in our class. Checks whether the overloaded property has value.

What is the act of the __unset() function to overload the property?

When an overloaded property is checked with the unset () function from the outside of class, Then the __unset() function is automatically executes in class. And allows the value of the overloaded property to be unset.

<?php
class PropertyTest
{
    /**  Location for overloaded data.  */
    private $data = array();
 
    public function __set($name, $value)
    {
        echo "Setting '$name' to '$value'\n";
        $this->data[$name] = $value;
    }
 
    public function __get($name)
    {
         echo "Getting '$name' Property with Value : ";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }
     
    public function __isset($name)
    {
        echo "Is '$name' Property set?\n";
        echo isset($this->data[$name])?"Yes '$name' Property is set":"No '$name' Property is Not set";
        echo "\n";
    }
 
    public function __unset($name)
    {
        echo "Unsetting '$name'\n";
        unset($this->data[$name]);
    }
 
}
 
 
$obj = new PropertyTest;
 
$obj->a = "Masud Alam";
echo $obj->a . "\n";
echo isset($obj->a);
unset($obj->a);
echo isset($obj->a);
echo "\n";
 
?>

Explanation: Notice, we have call unset() function at line number 42 to unset the value of a unknown property, With the unset ()function call, the __unset() function is automatically executed on line number 28 in our class. Unset the value of the overloaded property.

What is Method Overloading?

From outside of the class, according to class user demand, automatically creating any method (Which has not been define before) in class is called Property Overloading. We can also call Method Overloading as a way to automatically create method according to class user demand.

In Method overloading, we create an Overloaded Method from the outside of class with the __call() function and set the Parameter to overloaded method. Make an overloaded static method from the outside of the class with the __callStatic() function and set the Parameter to overloaded static method

<?php
class MethodTest
{
    public function __call($name, $arguments)
    {
        echo "Calling object method '$name' "
             . implode(', ', $arguments). "\n";
    }
 
    public static function __callStatic($name, $arguments)
    {
        echo "Calling static method '$name' "
             . implode(', ', $arguments). "\n";
    }
}
 
$obj = new MethodTest;
$obj->runTest('in object context');
 
MethodTest::runTest('in static context');
?>

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 *