PHP REPL - Interactive Editing Tool For Learning PHP (Hot Reload)
Tags: php, beginners, productivity, webdev
Published: 2019-11-09 05:35:47

When learning how to program, it is great to have a tool to write code and see the result instantly. If you have written JavaScript, you must have used a lot of tools like that. One notable mention is the developer console. Once you hit Enter, you can see the result of your code. 

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.

Let's see the end result:
![PHP Live reload](https://thepracticaldev.s3.amazonaws.com/i/lmj1pwaskeuvu23s7k8i.gif)

If you find it's interesting, keep reading!

# How to setup PHP live reload environment

## Install Python and PHP

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.
1. https://selenium-python.readthedocs.io/installation.html
2. https://pythonhosted.org/watchdog/installation.html

## Download geckodriver for selenium
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:
https://github.com/mozilla/geckodriver/releases

## Create PHP source folder
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. 

## Start PHP server from your source directory
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. 

## Clone PHP-REPL repo
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. 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/zn7py85xhh8ywyyfmixf.png)


Let's change the following details to match your own settings:
1. driver_path: this would be the path to the geckodriver you have downloaded above
2. my_url: the url you set when starting PHP server. In this case, that would be http://localhost:9000
3. php_source_path: path to your PHP source folder (created above).

Now, with all the settings entered. You are ready to start.

## Start Python
Now, cd to your PHP-REPL folder (where you have main.py) and simply type:
`python main.py`
or if you have multiple python versions installed, you may need to specifically call 
`python3 main.py`

and the python script is now watching your code changes. 

## Edit your code and have fun
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.

# Conclusion
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](https://www.binarycarpenter.com/) where I write about WooCommerce and WordPress