CodeIgniter Forums
Variable function names - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Variable function names (/showthread.php?tid=38891)



Variable function names - El Forum - 02-22-2011

[eluser]JonoB[/eluser]
Is it possible to use variables as function names?

Code:
$model = 'Product_model';
$function = 'count_products()';
$this->total_rows = $this->CI->$model->$function;

The model part seems to work fine, but it gets stuck at the $function part and php throws an error.

Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Products::$count_products()
Filename: core/Model.php
Line Number: 50

Is there any way of getting around this?


Variable function names - El Forum - 02-22-2011

[eluser]CroNiX[/eluser]
I think it will work if you put the parenthesis after the variable function name, not in the actual variable.
$model = 'Product_model';
$function = 'count_products';

$this->total_rows = $this->CI->$model->$function();

http://php.net/manual/en/functions.variable-functions.php


Variable function names - El Forum - 02-23-2011

[eluser]JonoB[/eluser]
Awesome sauce, thank you for your help!