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}();
}
you must specify a parameter that refers to the variable name you are trying to access.
Pass in $name
:
public function __get($name)
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.
The magic get function required exactly one argument, like the error said. Please try the following code:
public function __get( $name )
one argument is missing in __get($name) function. Which will give you the value of required variable.
public function __get($name) {
...
...
}