Call method

 <?php

//__call() is a Magic method.
//__call() method will automatically call when we try to call any Private method OR any Non
//Existing method.

class Student
{
    private $first_name;
    private $last_name;

    private function setName($fname, $lname)
    {
        $this->first_name = $fname;
        $this->last_name = $lname;
    }
    public function __call($method, $args)
    {
        echo "This is privated Method OR Non Existing Method : $method" . "<br>";
        print_r($args);
        echo "<br>";

        if(method_exists($this, $method))
        {
            call_user_func_array([$this, $method], $args);
        }
        else
        {
            echo "Method does not Exists : $method" . "<br>";
        }
    }
}
$test = new Student();
$test->setName("Yahoo", "Baba");    //Calling Private method here
echo "<br>";
$test->personal();  //Calling Non Existing method here
echo "<pre>";
print_r($test);
echo "</pre>";

//  OUTPUT:
//This is privated Method OR Non Existing Method : setName
// Array ( [0] => Yahoo [1] => Baba )

// This is privated Method OR Non Existing Method : personal
// Array ( )
// Method does not Exists : personal
// Student Object
// (
//     [first_name:Student:private] => Yahoo
//     [last_name:Student:private] => Baba
// )

?>

Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions