note 105331 added to function.create-function
| From: | aloha at ramatanzer dot com | Date: | Wed, 10 Aug 2011 00:58:49 +0000 |
| Subject: | note 105331 added to function.create-function | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182382@lists.php.net to get a copy of this message | ||
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"
----
Server IP: 64.71.164.2
Probable Submitter: 64.134.237.11
----
Manual Page -- http://www.php.net/manual/en/function.create-function.php
Edit -- https://master.php.net/note/edit/105331
Del: integrated -- https://master.php.net/note/delete/105331/integrated
Del: useless -- https://master.php.net/note/delete/105331/useless
Del: bad code -- https://master.php.net/note/delete/105331/bad+code
Del: spam -- https://master.php.net/note/delete/105331/spam
Del: non-english -- https://master.php.net/note/delete/105331/non-english
Del: in docs -- https://master.php.net/note/delete/105331/in+docs
Del: other reasons-- https://master.php.net/note/delete/105331
Reject -- https://master.php.net/note/reject/105331
Search -- https://master.php.net/manage/user-notes.php