Reputation: 10898
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
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
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
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
Reputation: 165
you must specify a parameter that refers to the variable name you are trying to access.
Upvotes: 1