I tried to find something similar for my PHP journey. Unfortunately, I couldn't find one.
I thought that the mechanism of a such tool is not complicated. You would need a service/program to watch the file changes. Once that happens, reload the code. In PHP case, that could be a browser.
Having working with python for a while, I know that is possible to watch files. I also know that with selenium, Python can launch and control a browser instance.
So I made this tool to implement hot reload for PHP.
If you find it's interesting, keep reading!
Before you begin, make sure you have Python and PHP installed on your computer. With Python, you also need to install selenium and watchdog. Don't worry, they are quite simple to install with pip.
geckodriver (or you can use chromium if you like) is needed to run a web browser from python. You can download the one for your OS here:
The next step is to create a folder where you write PHP. It could be any folder on your computer. You don't need to install XAMPP or WAMP or similar tools. PHP has capability to run a server by itself.
cd to your PHP source folder and type the following:
php -S localhost:9000
and hit enter.
You can change the port number (9000) to something else if you prefer.
This is the repo where I put the Python code that does the hot reload thing. When you have the repo on your desktop, open main.py with your favorite editor.
Let's change the following details to match your own settings:
- driver_path: this would be the path to the geckodriver you have downloaded above
- my_url: the url you set when starting PHP server. In this case, that would be http://localhost:9000
- php_source_path: path to your PHP source folder (created above).
Now, with all the settings entered. You are ready to start.
Now, cd to your PHP-REPL folder (where you have main.py) and simply type:
or if you have multiple python versions installed, you may need to specifically call
and the python script is now watching your code changes.
Finally, go to your PHP source folder and create a .php file (preferably index.php) and start writing your code.
Once you make changes (and save) to the file, a browser instance will run and display your code.
After that, every time you save your .php file, the browser will automatically reload.
I have been using this tool to overhaul my PHP knowledge and enjoy it so far. Hopefully it helps you with your journey too.
If you have questions/suggestions, please let me know.
Enjoy the post? Check out my blog at BinaryCarpenter.com where I write about WooCommerce and WordPress