note 105331 deleted from function.create-function by danbrown
| From: | danbrown@php.net | Date: | Wed, 10 Aug 2011 15:51:36 +0000 |
| Subject: | note 105331 deleted from function.create-function by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-182401@lists.php.net to get a copy of this message | ||
Note Submitter: aloha at ramatanzer dot com
Reason: useless
----
Hi All, I just figured out this little gem for dynamic loading of functions, after looking all over
the place, the notes on this page, pointed me in the right direction. Anyhow, I'm a big fan of
PHP and I figured, I'd contribute.
**This is my first post, ...enjoy :-)
// BEGIN SAMPLE PHP FILE
<?
// Sample Class
class sample
{
// Group Methods To Call With Ease
public $generic1;
// Class Constructor
function __construct($methods)
{
if ($methods)
{
foreach ($methods as $method)
{
// Path to generic PHP code file WITHOUT the open and close tags
$path = "your/path/to/" . $method . ".inc";
if (file_exists($path))
{
$code = file_get_contents($path);
if ($code)
{
// Create Function
$func = create_function("", $code);
// Store Method For Later Use
$this->generic1[$method] = $func;
}
}
}
}
}
}
// Load Methods by Array - array("func1","func2","func3") ..etc
$methods = array("test_code");
// INIT class
$sample = new sample($methods);
// ********************************************************
// Arguments
$arg1 = 'foo';
$arg2 = 'bar';
// Execute Method
echo $sample->generic1['test_code']($arg1, $arg2);
// Will Output: Hello World: foo | bar | 1312936517
// Have a Nice Day!
?>
// END SAMPLE PHP FILE
// ********************************************************
// BEGIN SAMPLE "test_code.inc" -- NOTE this file does not have "<?" or
"?>"
$a = '';
$b = '';
$args = func_get_args();
if (isset($args[0]))
{
$a = $args[0];
}
if (isset($args[1]))
{
$b = $args[1];
}
return "Hello World: " . $a . " | " . $b . " | " . time();
// END SAMPLE "test_code.inc"