sanders
sanders

Reputation: 10898

Problem with magic get method

I am trying to write my own magic __get method in php. But am getting this error: __get() must take exactly 1 argument. The error is referring to the last line in the function below.

Any idea's?

public function __get()
{
    $method = 'get'.ucfirst($name);
    if(!method_exists($this, $method))
            throw new Exception('Invalid property');
    var_dump($method);
    return $this->{$method}();

}

Upvotes: 0

Views: 956

Answers (6)

user479911
user479911

Reputation:

Change your first line to

public function __get($name) {

http://no.php.net/__get

Upvotes: 4

Gaurav
Gaurav

Reputation: 28755

one argument is missing in __get($name) function. Which will give you the value of required variable.

public function __get($name) {
...
...
}

Upvotes: 1

Martijn
Martijn

Reputation: 5611

The magic get function required exactly one argument, like the error said. Please try the following code:

public function __get( $name )

Upvotes: 2

Intrepidd
Intrepidd

Reputation: 20878

Is this better?

public function __get($name)
{
    $method = 'get'.ucfirst($name);
    if(!method_exists($this, $method))
            throw new Exception('Invalid property');
    var_dump($method);
    return $this->{$method}();

}

I added the name argument.

Upvotes: 2

chelmertz
chelmertz

Reputation: 20601

Pass in $name:

public function __get($name)

Upvotes: 2

Ali Rankine
Ali Rankine

Reputation: 165

you must specify a parameter that refers to the variable name you are trying to access.

Upvotes: 1

Related Questions