Welcome! Log In Create A New Profile

Advanced

Call method from string

Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Call method from string
April 15, 2013 11:18AM
Hi

How do I call a class method when the name will only be clear at runtime? I am sending all requests to index.php and parsing the url from there. the url would be formatted like this:
http://www.mysite.com/controller/method/queryString
I get it to load the controller but don't know how to load the method in that loaded cotroller. I tried this:
Language: PHP
//the controller loads, I put an echo in the constructor to make sure ${$controller} = new $controller;   //action is the method part of the url if($action != "") { if ((int)method_exists($controller, $action)) { //this evaluates to true and finds the function $controller->${$action}(); //but then an error here } else { echo "not a valid function"; //this executes when I delete the function from the controller } }

say for example this is my url htt p:// localhost /portfolio /main / bob
then it calls the main controller but doesn't call the bob function. this is the code for the main controller which is just to test to see if the framework loads classes.

Language: PHP
class Main extends Controller { function __construct() { echo "this is an instance of main<br />"; }   public function bob() { echo "You called?"; } }

----------------------------------------------------------------------------------------

int get_random_number() {
return 4;
}
avatar Re: Call method from string
April 15, 2013 12:16PM
I got it smileys with beer

needed to remove the { } from the function and add them to the controller like this:

Language: PHP
$controller->${$action}(); //wrong!!   ${$controller}->$action(); //works

guess I should have asked earlier. haha

----------------------------------------------------------------------------------------

int get_random_number() {
return 4;
}
Sorry, only registered users may post in this forum.

Click here to login