My code is like this:
$value) $this->{$key}= $value; The Public Function __call ($methodName, $params) {$params =array_merge (' stdobject ' = $this), $params); if (Isset ($this->{$methodName}) &&is_callable ($methodName)) {//$params is the parameter of the function, where the stdobject is In the $stdobject; Return Call_user_func_array ($methodName, $params); } else {throw new Exception ("Fatal error:call to undefined method stdobject::{$methodName} ()"); }}}echo phpversion (); $obj = new Stdobject (); $obj->name = "Nick"; $obj->surname = "Doe"; $obj->age =; $obj-&G T;adresse = null, $obj->getinfo = function ($stdObject) {//$stdObject referred to this object (Stdobject). Echo $stdObject->name. " " . $stdObject->surname. "Have". $stdObject->age. "yrs old." and live in ". $stdObject->adresse;}; Print_r ($obj); $obj->getinfo ();? >
First, as a matter of principle, call a class that does not exist in the middle of the member variable, because the display of the call magic method _set , but there is no way to display the declaration _set method;
2. Why can't I call the $obj->getinfo () method? An exception is thrown once the call is called
'Fatal error: Call to undefined method stdObject::getInfo(), it means that this method does not exist.
Reply content:
My code is like this:
$value) $this->{$key}= $value; The Public Function __call ($methodName, $params) {$params =array_merge (' stdobject ' = $this), $params); if (Isset ($this->{$methodName}) &&is_callable ($methodName)) {//$params is the parameter of the function, where the stdobject is In the $stdobject; Return Call_user_func_array ($methodName, $params); } else {throw new Exception ("Fatal error:call to undefined method stdobject::{$methodName} ()"); }}}echo phpversion (); $obj = new Stdobject (); $obj->name = "Nick"; $obj->surname = "Doe"; $obj->age =; $obj-&G T;adresse = null, $obj->getinfo = function ($stdObject) {//$stdObject referred to this object (Stdobject). Echo $stdObject->name. " " . $stdObject->surname. "Have". $stdObject->age. "yrs old." and live in ". $stdObject->adresse;}; Print_r ($obj); $obj->getinfo ();? >
First, as a matter of principle, call a class that does not exist in the middle of the member variable, because the display of the call magic method _set , but there is no way to display the declaration _set method;
2. Why can't I call the $obj->getinfo () method? An exception is thrown once the call is called
'Fatal error: Call to undefined method stdObject::getInfo(), it means that this method does not exist.
First of all, who made the indentation, quickly stand out, I promise not to kill you oh ... Also answer your question:
You know how to call a magic method when you call an inaccessible member variable __set() , so why don't you define it! You don't define how you make people work, it's like you don't give me the job, but do you want me to do it like a dog every day? Don't tease me, okay? Please add your own Magic method definition.
class stdObject { private function __set($name, $value) { $this->$name = $value; }}
- The definition of member properties and member functions in PHP is not the same, I have not seen the use of
$this->method = function() {} this definition of member functions, you are the idea of JavaScript into PHP. So when you invoke an inaccessible member function, the __call() magic method is triggered, your isset() judgment is true, but it is_callable() will return False (and here is a reminder of a clerical error, yes is_callable($this->$methodName) is_callable($methodName) , two is not exactly the same!) You think you put the variable name methodName you don't give $this the program will automatically call you a member function! What about the pit Daddy!! ), and then your syntax throws an exception out.
Dude you are teasing me. I will is_callable($methodName) turn into is_callable($this->{$methodName}) a successful run. My reason is wrong here. Rather than the Magic method. and achieved the effect I wanted. Now the code is this:
$value) {$this->{$key}= $value; }}} Public function __call ($methodName, $params) {$params =array_merge (array (' Stdobject ' => ; $this), $params); if (Isset ($this->{$methodName}) &&is_callable ($this->{$methodName})) {//$params is the parameter of the function, The stdobject here is the $stdobject in function; Return Call_user_func_array ($this->{$methodName}, $params); } else{throw new Exception ("Fatal error:call to undefined method stdobject::{$methodName} ()"); }}}; $obj = new Stdobject (); $obj->name = "Nick"; $obj->surname = "Doe"; $obj->age =; $obj->adresse = null;$ Obj->getinfo = function ($stdObject) {//$stdObject referred to this object (Stdobject). Echo $stdObject->name. " " . $stdObject->surname. "Have". $stdObject->age. "yrs old." and live in ". $stdObject->adresse;}; $obj->getinfo ();? >
The result of the operation is:
Nick Doe have 20 yrs old. And live in
First of all, I want to say that the foundation of dude is not very solid. $this->method = function () {} Of course you can define member functions like this. Also, calling a non-existent property does not necessarily show the specified _set method.
--------------------------------------------------------------------------------------------------------------- You know why I'm doing this? Because this is what the official PHP Handbook says:
[#1] Ashley Dambra [2014-02-21 06:50:10]
Here a simple class ' stdobject ', give us the possibility to create dynamic classes and the possibility to add and exec Ute methods thing that's StdClass ' don ' t let us do. Very useful if you extends it to a controller on MVC Design pattern. Let users create own classes.
I have the also post this class on http://www.php.net/manual/en/language.types.object.php
$argument) {$this->{$property} = $argument; }}} Public function __call ($method, $arguments) {$arguments = Array_merge (Array ("stdobject" = $ This), $arguments); Note:method argument 0 would always be referred to the main class ($this). if (Isset ($this->{$method}) && is_callable ($this->{$method}) {return Call_user_func_array ($this ->{$method}, $arguments); } else {throw new Exception ("Fatal error:call to undefined method stdobject::{$method} ()"); }}}//Usage. $obj = new Stdobject (); $obj->name = "Nick"; $obj->surname = "Doe"; $obj->age =; $obj->adresse = null; $obj->getinfo = function ($stdObject) {//$stdObject referred to this object (Stdobject). Echo $stdObject->name. " " . $stdObject->surname. "Have". $stdObject->age. "yrs old." and live in ". $stdObject->adresse;}; $func = "Setage"; $obj->{$func} = function ($stdObject, $age) { $age is the first parameter passed if calling this method. $stdObject->age = $age;}; $obj->setage (24); Parameter value passing to the $age argument in method ' Setage () './/Create dynamic method. Here I ' m generating getter and setter dynimically//Beware:method name is case Sensitive.foreach ($obj as $func _name =&G T $value) {if (! $value instanceof Closure) {$obj->{"set". Ucfirst ($func _name)} = function ($stdObject, $value Use ($func _name) {//note:you can also use keyword ' use ' to bind parent variables. $stdObject->{$func _name} = $value; }; $obj->{"Get". Ucfirst ($func _name)} = function ($stdObject) use ($func _name) {//note:you can also use keyword ' use ' To bind parent variables. return $stdObject->{$func _name}; }; }} $obj->setname ("John"), $obj->setadresse ("Boston"); $obj->getinfo (); >