mannyee Posted October 12, 2010 Share Posted October 12, 2010 i have a constructor public function __construct(){ // check if any action related to this page was generated if(isset($_GET['action'])){ $this->action = ucwords($_GET['action']) . '()'; self::{$this->maction}; } //code to generate the list of all the systems users $user = new userDAO; $this->results = $user->fetchAll(); } if action is present in the querystring, eg. delete, it would be stored into $this->action as Delete(). Similarly, if the action was edit, it would be stored as Edit(). My intent is to call the method named Delete() or Edit() depending upon which action was generated. I have defined the methods in the same class. Once i assign the current action to the $this->action, i want to call the method without explicitly specifying the method name.... here i have tried self::$this->action.... i even tried $this->action only but its not working? i think the string stored in the self::$this->action is not being interpolated? or....sth. i dont know. pls help out guys Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/ Share on other sites More sharing options...
rwwd Posted October 12, 2010 Share Posted October 12, 2010 self::{$this->maction}; I hope as that was a typo! I think that when you use self::action you do this instead of $this->action, but I'm not sure, I would have to read up on that to see, either will result in the same action so your syntax is more than likely wrong, probably should be:- self::action; Though I am probably wrong there, I seldom use self:: but I do use the 'scope resolution operator' :: outside the class to call functions statically.. Rw Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121410 Share on other sites More sharing options...
trq Posted October 12, 2010 Share Posted October 12, 2010 I don't see any reason to store action for later use within the object. if (isset($_GET['action'])){ $action = ucwords($_GET['action']); $this->$action();} Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121412 Share on other sites More sharing options...
mannyee Posted October 12, 2010 Author Share Posted October 12, 2010 well...lets not store the action in the object property... i just want the action received from the query string to invoke the related method in the class.... if i got edit (or delete) action, i want it to be formatted into Edit() or Delete(), which i have done using ucwords($_GET['action']) . '()'; How can i invoke the relevant method? Currently, i've found out a solution just to please my boss... public function __construct(){ //check if any action related to this page was generated if(isset($_GET['action']{ if($_GET['action'] == 'delete'){ self::Delete(); } if($_GET['action'] == 'edit'){ self::Edit(); } } } but i dont want to use if clause, i want to optimize it in such a way that when an action is received, we capitalize the first letter, concat () and invoke the relevant method.... Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121416 Share on other sites More sharing options...
ignace Posted October 12, 2010 Share Posted October 12, 2010 class SomeClass { function __call($method, $args) { if(method_exists($this, $method)) return $this->$method(); return NULL; } function call($method, $args) { return $this->__call($method, $args); }} $class = new SomeClass();$output = $class->call($_GET['action']); Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121418 Share on other sites More sharing options...
trq Posted October 12, 2010 Share Posted October 12, 2010 but i dont want to use if clause, i want to optimize it in such a way that when an action is received, we capitalize the first letter, concat () and invoke the relevant method.... Look at my example again. Don't bother adding the () as part of the string/variable, there not. Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121453 Share on other sites More sharing options...
mannyee Posted October 13, 2010 Author Share Posted October 13, 2010 hi thorpe!! thanks very much for your ans..it did indeed work for me. in the mean time, your solution raised a new curiousity, will i be able to access this constructor's local variable $action from another method in the class, like: function Method(){ echo $action; } $obj = new Class(); $obj->Method(); // so that $action is printed i tried it and got a php notice: Notice: Undefined variable: b in C:\wamp\www\test\index.php on line 31 my idea is that since $action variable is part of the constructor, it must be included in the $obj unlike other private public and protected properties when we instantiate an object for the Class(). isn't it so? Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121679 Share on other sites More sharing options...
trq Posted October 13, 2010 Share Posted October 13, 2010 $action doesn't exist within other methods of your class. You would need to define it local to the object itself. class Foo { private $action; public function __construct() { $this->action = 'hello'; } public function test() { echo $this->action; } } $obj = new Foo(); $obj->test(); Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121684 Share on other sites More sharing options...
mannyee Posted October 13, 2010 Author Share Posted October 13, 2010 many a thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/215690-method-calling-using-self/#findComment-1121691 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.