Jump to content

How to get the code of a function?


MrGary

Recommended Posts

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..

Link to comment
Share on other sites

I did that  :D

 

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 :D

 

Anyway, thank you everyone :)

Link to comment
Share on other sites

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 :D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.