alber Posted October 27, 2009 Share Posted October 27, 2009 Hello. I can define properties (data members) but I cant define a method for an object belongs to stdClass(generic) class. for example : $myObject = new stdClass(); $myObject->prop1 = "hello"; // works OK function fun1() { echo "bye"; } $myObject->method1 = fun1; // object accept the asignament $myObject->metodo1(); the last sentence show an error : server message : Fatal error: Call to undefined method stdClass::metodo1() in /home/.../public_html/pru/objetos.php on line 63 Thanks for any help. Alber Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/ Share on other sites More sharing options...
waynew Posted October 27, 2009 Share Posted October 27, 2009 Are you sure that you've not made a mistake with your spelling: $myObject->method1 = fun1; // object accept the asignament $myObject->metodo1(); Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945594 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 $myObject->method1 = fun1; // object accept the asignament No it doesn't. It just sets $myObject->method1 to "fun" (string) or assigns it a value of 'fun' constant if such constant is defined. As far as I know you cannot add methods dynamically to an object. Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945596 Share on other sites More sharing options...
alber Posted October 27, 2009 Author Share Posted October 27, 2009 Are you sure that you've not made a mistake with your spelling: $myObject->method1 = fun1; // object accept the asignament $myObject->metodo1(); sorry. I want to translate to english and forgot change the "metodo1 to method1. But the question in the same. $myObject->method1(); //it doesn work Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945601 Share on other sites More sharing options...
alber Posted October 27, 2009 Author Share Posted October 27, 2009 $myObject->method1 = fun1; // object accept the asignament No it doesn't. It just sets $myObject->method1 to "fun" (string) or assigns it a value of 'fun' constant if such constant is defined. As far as I know you cannot add methods dynamically to an object. bad news. It is useful to define object without a custom class like in javascript. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945603 Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 As far as I know you cannot add methods dynamically to an object. class Foo {} $foo = new Foo(); $foo->bar = function() { echo 'test'; }; I wouldn't recommend that kind of code though. It is useful to define object without a custom class like in javascript. Javascript uses prototype based object orientation while PHP uses class based object orientation. Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945604 Share on other sites More sharing options...
mikesta707 Posted October 27, 2009 Share Posted October 27, 2009 For the stdObject, I'm not sure. For your own defined class, you can use the magic __call method, and create a static registerMethod method to dynamically add functions. consider the following class Dynamic { //This is an array with all the classes methods. we can define the methods outside the class static protected $methods = array(); //this will register any method we want to the class, by adding it to the array public static function registerMethod($method) { self::$methods[] = $method; } //this is where the "magic" happens. This function gets called when you call the method //it will see if the function is in the methods array, and then call it private function __call($method, $args) { if (in_array($method, self::$methods)) { return call_user_func_array($method, $args); } } } //example //define a simple function function test() { print "Hello World"; } //call our static method to add the test function to the class Dynamic::registerMethod('test'); //instantiate the class $d = new Dynamic(); //now call the method $d->test(); if you wish to read more about it, Here is where I got the idea from, and you can also view the rest of PHPs magic methods at the manual As far as I know you cannot add methods dynamically to an object. class Foo {} $foo = new Foo(); $foo->bar = function() { echo 'test'; }; I wouldn't recommend that kind of code though. I actually tried something similar to this at first, and it didn't work. I copy pasted this also, and it doesn't work. error: Parse error: syntax error, unexpected T_FUNCTION in C:\Server\htdocs\text.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945614 Share on other sites More sharing options...
salathe Posted October 27, 2009 Share Posted October 27, 2009 class Foo {} $foo = new Foo(); $foo->bar = function() { echo 'test'; }; Just to clarify (it might not be clear for the OP), Daniel's code still does not define the 'bar' method, it assigns an anonymous function to the bar property (which might be fine). Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945617 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 I actually tried something similar to this at first, and it didn't work. I copy pasted this also, and it doesn't work. error: Parse error: syntax error, unexpected T_FUNCTION in C:\Server\htdocs\text.php on line 8 Anonymous functions are available in PHP5.3+ Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945629 Share on other sites More sharing options...
salathe Posted October 27, 2009 Share Posted October 27, 2009 class Foo {} $foo = new Foo(); $foo->bar = function() { echo 'test'; }; Also note that you won't be able to call $foo->bar() — you need to use call_user_func($foo->bar) or $foo->bar->__invoke() Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945644 Share on other sites More sharing options...
mikesta707 Posted October 27, 2009 Share Posted October 27, 2009 I actually tried something similar to this at first, and it didn't work. I copy pasted this also, and it doesn't work. error: Parse error: syntax error, unexpected T_FUNCTION in C:\Server\htdocs\text.php on line 8 Anonymous functions are available in PHP5.3+ ahh ok. I always wondered when they would add those in. Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945649 Share on other sites More sharing options...
Daniel0 Posted October 27, 2009 Share Posted October 27, 2009 Also note that you won't be able to call $foo->bar() — you need to use call_user_func($foo->bar) or $foo->bar->__invoke() Yeah, but to be honest, I consider that a bug more than a feature. When $bar('something') is callable, then $foo->bar('something') should be as well (assuming both refer to anonymous functions). Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945652 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 It also seems that method created this way can't use $this or self:: I'm toying with it right now, so I might be wrong Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945658 Share on other sites More sharing options...
salathe Posted October 27, 2009 Share Posted October 27, 2009 Yeah, but to be honest, I consider that a bug more than a feature. When $bar('something') is callable, then $foo->bar('something') should be as well (assuming both refer to anonymous functions). It would be much more intuitive to be able to do that, I agree. It also seems that method created this way can't use $this or self:: I'm toying with it right now, so I might be wrong There is no concept of $this or self:: in these anonymous functions. You could pass the object ($foo) through to the function (in place of $this) using use and have Foo:: (in place of self::) but it's not a pretty solution. Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945675 Share on other sites More sharing options...
Mchl Posted October 27, 2009 Share Posted October 27, 2009 Yeah... Note to self: just don't do it. Quote Link to comment https://forums.phpfreaks.com/topic/179227-method-creation-on-generic-object/#findComment-945680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.