MrGary Posted January 18, 2012 Share Posted January 18, 2012 Hello, I just wanted to know if it was possible to get the code of a function created inside a PHP file, for example : <?php function test() { $var = 0; echo $var++; } ?> I want to get the code inside the function, means : <?php $string = function_get_content("test"); /* $string = "$var = 0; echo $var++;" */ ?> Is that possible in any way? I believe that I'd have to analyze the page's code :s Thanks in advance.. Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/ Share on other sites More sharing options...
Muddy_Funster Posted January 18, 2012 Share Posted January 18, 2012 could you post the code for function_get_content()? Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309008 Share on other sites More sharing options...
ManiacDan Posted January 18, 2012 Share Posted January 18, 2012 No, you cannot do that. What problem are you trying to solve here? What are you actually trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309011 Share on other sites More sharing options...
MrGary Posted January 18, 2012 Author Share Posted January 18, 2012 I did that I feel so proud of me, actually I'm building a new way of implementing plugins into a script... that consists of methods and hooks... You program your method EXACTLY AS IF you were programming in the core (no need to get parameters or anything), and then you just add your function to the hook you want it to be implemented in for example : If my core code is : <?php $code = 0; run_hooks("hook_here"); echo $code; ?> And now let's say that I want to create a plugin that increments the $code variable before the output, I will just and put in the plugin : <?php function install() //this is the function called while the install { add_hook("hook_here", "function_1"); //adding the function_1() to the "hook_here"... } /* The problem is, I need to do something like $code = function_1() to increment it... because calling function_1() only won't increment the $code variable, or I need to pass it by reference which is deprecated and complex */ /* So I will do : */ function function_1() { //exactly as if I were programming directly in the core $code++; } ?> run_hooks() gets the code of the functions added, and eval() it directly in the core... So you don't edit any core files, and you don't create complex HOOKS Anyway, thank you everyone Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309021 Share on other sites More sharing options...
ManiacDan Posted January 18, 2012 Share Posted January 18, 2012 That code in your example won't work, actually. $code will have lexical scope. Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309026 Share on other sites More sharing options...
MrGary Posted January 18, 2012 Author Share Posted January 18, 2012 Oh it worked I did this : <?php $code = 0; $return = get_function_code("Plugin_Name", "Method_Name"); /* "Plugin_Name" is the class of my plugin, "Method_Name" is the name of my method.. get_function_code() returns the code of the method "Method_Name" of the class "Plugin_Name" */ eval("$return;"); echo $code; //echo'd 1 ?> Tadaaaa Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309032 Share on other sites More sharing options...
ManiacDan Posted January 18, 2012 Share Posted January 18, 2012 Eval() is not supposed to be used in real applications, it's for debugging and testing and stuff. Why can't you just...call this function? I still don't know what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309039 Share on other sites More sharing options...
MrGary Posted January 18, 2012 Author Share Posted January 18, 2012 Nevermind, but eval() isn't used for debugging only... Many big softwares use it in their code and it's in many cases the only possible way to do things... Thanks anyway Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309046 Share on other sites More sharing options...
scootstah Posted January 18, 2012 Share Posted January 18, 2012 Nevermind, but eval() isn't used for debugging only... Many big softwares use it in their code and it's in many cases the only possible way to do things... Thanks anyway It is very poor practice to use eval in this way. There are a few rare circumstances where eval is good to use. This isn't one of them. If you want your function to alter variables like that, just use global variables. It's still bad practice, but better than eval. Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309049 Share on other sites More sharing options...
ManiacDan Posted January 18, 2012 Share Posted January 18, 2012 If you think it's the only want to do it, either you're wrong or there's a better way to do what you're trying to do. For instance, call these functions (and write them properly) instead of loading them as a string and eval'ing them. Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309053 Share on other sites More sharing options...
ManiacDan Posted January 18, 2012 Share Posted January 18, 2012 Or, instead of putting them in useless functions that you won't use, put them in files and include() those files. I've thought of two other ways to do this without using the (incredibly stupid and dangerous) eval() Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309055 Share on other sites More sharing options...
RussellReal Posted January 19, 2012 Share Posted January 19, 2012 try this instead <?php $availableHooks = array( 'init' => array(), 'exit' => array() ); function add_hook($mainHook,$appendFunction) { global $availableHooks; $availableHooks['init'][] = $appendFunction } function init($functions) { foreach ($functions as $func) { $func(); } } function exit($functions) { foreach ($functions as $func) { $func(); } } function run_hooks($hook) { global $availableHooks; if (isset($availableHooks[$hook])) $hook($availableHooks[$hook]); } ?> when adding a hook: <?php function runThis() { mysql_query("alalalalala"); } add_hook('init','runThis'); ?> then when its time for a hook: <?php run_hooks('init'); ?> that would be a million times better, and yes I know it uses global, sure you could probably avoid that, but I'm writing this fast eval() is bad Quote Link to comment https://forums.phpfreaks.com/topic/255311-how-to-get-the-code-of-a-function/#findComment-1309187 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.