PHP overloading ( method overloading and property overloading)


Hello All

Today I will focus on overloading method and property in PHP. Unlike other language Java/C# overloading is working in a little bit different way for PHP.

Normally we can overload a method by providing different/varying argument(s) ,but in PHP it should have to create through some magic methods.

These magic methods are as bellow:

(For overloading property)

void __set ( string $name , mixed $value )

mixed __get ( string $name )

bool __isset ( string $name )

void __unset ( string $name )

(For overloading method)

mixed __call ( string $name , array $arguments )

mixed __callStatic ( string $name , array $arguments )

It will be better to describe the operation inside code comment rather that bookish thing hear. Please go through the code comment to get know what is gonging over there to perform overloading .


<?php

class PhpOverloading {
  
  /* Property overloading -start- */
  private $_data = array ();
  
  public function __set($name, $value) {
    /*
     * set the key value in the $_data private
     * variable.
     * */
    $this->_data [$name] = $value;
  }
  
  public function __get($name) {
    /*
     * Retrieve the value of key which we have
     * set through __set method.
     * */
    return $this->_data [$name];
  }
  
  public function __isset($name) {
    /*
     * Check wheather the key exist or not. 
     * */
    return isset ( $this->_data [$name] );
  }
  
  public function __unset($name) {
    /*
     * Unset the value of key.
     * */
    unset ( $this->_data [$name] );
  }
  
  /* Property overloading -end- */
  
  /* Method overloading -start- */
  public function __call($name, $argument) {
    /*
     * perform your operation as needed,
     * I just used a switch case to show how 
     * we can set the dynamic method with parameters.
     * */
    
    switch ($name) {
      case ('First') :
        echo "This is the first dyanamic method and arguments are " . implode ( ',', $argument );
        break;
      case ('Second') :
        echo "2nd method and the arguments are {$argument[1]} and {$argument[2]}";
        break;
      default :
        echo "Method unhandled with the name {$name} and arguments are " . implode ( ',', $argument );
    }
  }
  
  public static function __callstatic($name, $argument) {
    
    /*
     * It's similar as __call but works as static method
     * call. 
     * */
    switch ($name) {
      case ('FirstStatic') :
        echo "Static first dyanamic method and arguments are " . implode ( ',', $argument );
        break;
      case ('SecondStatic') :
        echo "Static 2nd method and the arguments are {$argument[1]} and {$argument[2]}";
        break;
      default :
        echo "Static method unhandled with the name {$name} and arguments are " . implode ( ',', $argument );
    }
  }
  
/* Method overloading -end- */
}

$phpOverloading = new PhpOverloading ();

echo 'Property overloading output -start-';
echo "<br /><br />";
var_dump ( isset ( $phpOverloading->dynamicProperty ) );
$phpOverloading->dynamicProperty = "new property injected";

echo "<br />";
echo $phpOverloading->dynamicProperty;
echo "<br />";

var_dump ( isset ( $phpOverloading->dynamicProperty ) );

unset ( $phpOverloading->dynamicProperty );

echo "<br />";
var_dump ( isset ( $phpOverloading->dynamicProperty ) );
echo "<br /><br />";

echo 'Property overloading output -end-';

echo "<br /><br />";

echo 'Method overloading output -start-';

echo '<br /><br />';
$phpOverloading->First ( 'one', 'two', 'three' );
echo "<br />";
$phpOverloading->Second ( '1', '2' );
echo "<br />";
$phpOverloading->UnHandledMethod ( '1', '2' );
echo "<br /><br />";

echo 'staic method overloading';
echo "<br /><br />";
PhpOverloading::FirstStatic ( 'one', 'two', 'three' );
echo "<br />";
PhpOverloading::SecondStatic ( '1', '2' );
echo "<br />";
PhpOverloading::UnHandledStaticMethod ( '1', '2' );
echo "<br /><br />";

echo 'Method overloading output -end-';

/*
 * final output is as bellow:
 
Property overloading output -start-

bool(false) 
new property injected
bool(true) 
bool(false) 

Property overloading output -end-

Method overloading output -start-

This is the first dyanamic method and arguments are one,two,three
2nd method and the arguments are 2 and 
Method unhandled with the name UnHandledMethod and arguments are 1,2

staic method overloading

Static first dyanamic method and arguments are one,two,three
Static 2nd method and the arguments are 2 and 
Static method unhandled with the name UnHandledStaticMethod and arguments are 1,2

Method overloading output -end-

 * 
 * */

You may check my other posts from my [ Blog ] .

Please provide your feedback.

Thanks

That’s all for today.

BYE


Leave a Reply