droberts Posted January 6, 2009 Share Posted January 6, 2009 I have a method of a class which should assign an instance variable to that object: class testklass { public function hello() { $this->amihere = 'assigned in class'; } } This correctly assigns the amihere variable: $k1 = new testklass(); $k1->hello(); print_r($k1); // Object ( [amihere] => assigned in class ) This does not: $name = 'testklass'; $k2 = new $controller(); call_user_func_array(array($k2 , 'hello'),null); print_r($k2); // Object ( ) Is there a way around this? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 I do not think you can dynamically instantiate a class like that with $controller....I also do not see a "controller" class. $name is not being used anywhere either..... Quote Link to comment Share on other sites More sharing options...
droberts Posted January 7, 2009 Author Share Posted January 7, 2009 Sorry for the confusion, I'm not in front of my php code and I mis-typed: $controller = 'testklass'; $k2 = new $controller(); call_user_func_array(array($k2 , 'hello'),null); print_r($k2); // Object ( ) $controller is the variable, not $name, and this code indeed does run, but the instance variable is not being set from the method call Quote Link to comment Share on other sites More sharing options...
DarkWater Posted January 7, 2009 Share Posted January 7, 2009 <?php class testklass { public function hello() { $this->amihere = 'assigned in class'; } } $controller = 'testklass'; $k2 = new $controller(); call_user_func_array(array($k2 , 'hello'),array()); print_r($k2); // Object ( ) Works for me. Version: PHP 5.3.0alpha3 (cli) (built: Jan 3 2009 23:19:19) Quote Link to comment Share on other sites More sharing options...
droberts Posted January 7, 2009 Author Share Posted January 7, 2009 You are correct. I accidentally recreated the object and the new object obviously didn't have the property. Thanks Quote Link to comment 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.