Sachin Sudhakar Sonawane
Sachin Sudhakar Sonawane

Reputation: 1907

magic function __call function is not working

I am trying to work on Overloading using magic function

Here is my code:

class file1

    class vLiteUser{
        public function __call($methodname,$arguments)
        {
            if($methodname=='UserLogin'){
                switch(count($arguments)){
                    case 1:
                                $this->UserLogin($arguments[0]);
                        break;
                    case 2:
                                $this->UserLogin($arguments[0],$arguments[1]);
                        break;
                    default:    echo "string";
                                break
                }
            }   
        }    

    public function UserLogin($data0='')
    {
        echo $data0;
    }

    public function UserLogin($data0='',$data2='')
    {   
            echo $pass
    }
} ?>

I have created object in another file

 $userObj = new vLiteUser();
 $userObj->UserLogin(data0,data1);
 $userObj->UserLogin(data0);

My be something I have missing and not able to find what exactly it is

Also I want to ask is private functions also covered in overloading.

Upvotes: 2

Views: 3393

Answers (1)

yivi
yivi

Reputation: 47329

You can't have multiple definitions for a method in the same class.

This is wrong, and wont work no matter what visibility (public/private) you set to these methods:

public function UserLogin($data0='')
{
    echo $data0;
}

public function UserLogin($data0='',$data2='')
{   
    echo $pass
}

Besides, the magic method __call() will only work for undefined methods, or for methods not visible in the current scope.

So you either delete both function UserLogin, or you delete only one of them and make the other private.

Have some docs with that.

Upvotes: 6

Related Questions