脚本运行时是否可以动态地重新加载PHP代码? [英] Is it possible to dynamically reload PHP code while script is running?

查看:389
本文介绍了脚本运行时是否可以动态地重新加载PHP代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PHPSockets的多人服务器,因此完全用PHP编写.

I'm having a multiplayer server that's using PHPSockets, and thus is written entirely in PHP.

当前,每当我对PHP服务器脚本进行任何更改时,都必须终止该脚本,然后重新开始.这意味着所有在线用户都已断开连接(通常不会出现问题,因为目前没有多少用户).

Currently, whenever I'm making any changes to the PHP server-script I have to kill the script and then start it over again. This means that any users online is disconnected (normally not a problem because there aren't so many at the moment).

现在,我正在重写服务器脚本,以使用自定义PHP类并对内容进行一些整理(您不希望知道它今天看起来多么讨厌).今天我在想:难道不必重新启动整个脚本就可以对php源进行更改吗?".

Now I am rewriting the server-script to use custom PHP classes and sorten things up a little bit (you don't want to know how nasty it looks today). Today I was thinking: "Shouldn't it be possible to make changes to the php source without having to restart the whole script?".

例如,我计划有一个main.php文件,其中包括user.phpgame.php文件,其中user.php包含类MyUser,而game.php包含类MyGame. 现在,我想对user.php进行更改并重新加载"服务器,以便对user.php所做的更改在不断开任何在线用户的情况下生效吗?

For example, I'm planning on having a main.php file that is including user.php which contains the class MyUser and game.php which contains the class MyGame. Now let's say that I would like to make a change to user.php and "reload" the server so that the changes to user.php goes into effect, without disconnecting any online users?

我试图找到其他答案,我得到的最接近的问题是:

I tried to find other questions that answered this, the closest I got is this question: Modifying a running script and having it reload without killing it (php) , which however doesn't seem to solve the disconnection of online users.

更新

我对此的解决方案是:

  1. 在特殊场合,包括文件external.php,该文件可以访问一些变量,并根据需要使用它们.这样做时,我必须确保代码中没有错误,因为如果尝试访问不存在的方法,整个服务器将崩溃.
  2. 将整个内容重写为Java ,这使我可以使用动态类重载来添加插件系统.奇迹般有效.再见PHP.
  1. At special occations, include the file external.php, which can access a few variables and use them however it'd like. When doing this, I had to make sure that there were no errors in the code as the whole server would crash if I tried accessing a method that did not exist.
  2. Rewrite the whole thing to Java, which gave me the possibility of adding a plugin system using dynamic class reloading. Works like a charm. Bye bye PHP.

推荐答案

是否不必重新启动整个脚本就可以对php源进行更改?

Shouldn't it be possible to make changes to the php source without having to restart the whole script?

[...]

我正计划建立一个包含user.php的main.php文件 其中包含类MyUser

I'm planning on having a main.php file that is including user.php which contains the class MyUser

就您而言,您不能.类只能在运行的脚本中定义一次.您需要重新启动脚本以重新定义这些类.

In your case, you can't. Classes can only be defined once within a running script. You would need to restart the script to have those classes redefined.

这篇关于脚本运行时是否可以动态地重新加载PHP代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆