I'm working on a PHP IRC Bot, and I'm currently working on the commands.
At the beginning of main.php
, the script that starts the bot, it includes the class_lib.php
file and instantiates the object of that class. I want to work on a !reload
command, where it would "uninclude" the file and then reinclude it. Would that be possible, or would it be fine if I just include
d it again when that command was sent?
EDIT: Basically, I want to be able to modify and reload the class without having to restart the bot.
Why not just allow the object to reload the default settings, or restart, instead of what you describe? I'm pretty sure you can't do that anyways.
Also, don't try to load the object with the class_lib.php. Include the class file with the object, then where and when you need it, create your object. That way you can stop it, destroy the object, and then re-instantiate another object, which should accomplish what you want.
Am afraid there is no way to uninclude a file. If it is a function, you could generate a new function dynamically each time.
Check:
http://php.net/manual/en/function.create-function.php
You can assign a function to a variable and then clear that variable and assign it again.
No, you can't. Revisit your design. Don't couple the definition of the class with the instantiation of the object.
Late answer..
You could create an array containing the files and then eval(file_get_contents($filename))
I've been working on my own IRC Client named PITC and that may be the method I might use, It's how i've done stuff before
The question is essentially about developing dynamicly loadable php plugins in php that don't cause interference with other plugins or the main code.
The only way I know of would require you to redesign the original class_lib.php and create a 'Plugin' class that would act as a proxy class that would on every function/method call execute a new php instance and pass along the function arguments with a json_decode call and then echo the response with json_encode.
function proxyExternalFunction($fileName, $functionName, $args, $setupStatements = '') {
$output = array();
$command = $setupStatements.";include('".addslashes($fileName)."');echo json_encode(".$functionName."(";
foreach ($args as $arg) {
$command .= "json_decode('".json_encode($arg)."',true),";
}
if (count($args) > 0) {
$command[strlen($command)-1] = ")";//end of $functionName
}
$command .= ");";//end of json_encode
$command = "php -r ".escapeshellarg($command);
exec($command, $output);
$output = json_decode($output,true);
}
this would have to be done for every single function/method call via a proxy class... not so efficient, and it would never work for objects/associative arrays that hold references to resources or other unserializable variable references.
Another more efficient option would be http://www.php.net/runkit which allows you to interact with a long running embedded php instance.
Late to the party, but I have to mention the following PHP-extension:
http://www.php.net/manual/en/book.runkit.php
The runkit extension provides means to modify constants, user-defined functions, and user-defined classes. It also provides for custom superglobal variables and embeddable sub-interpreters via sandboxing.
This would allow you to re-define classes, which happens upon parsing included files.
NOTE: I have not used this, I came upon it after researching if it was possible to somehow hotswap certain classes. I cannot vouch for the safety and can not supply hands-on info. Yet.