开发者

is there a way to "uninclude" a file with PHP?

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 start开发者_高级运维s 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 included 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.


No, you can't. Revisit your design. Don't couple the definition of the class with the instantiation of the object.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜