DEV Community

Cover image for PHP REPL - Interactive Editing Tool For Learning PHP (Hot Reload)
Mạnh Đạt
Mạnh Đạt

Posted on

PHP REPL - Interactive Editing Tool For Learning PHP (Hot Reload)

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

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

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 where I write about WooCommerce and WordPress

Discussion (5)

Collapse
moopet profile image
Ben Sinclair

There's always php -a which gives you a REPL (though it's not particularly brilliant).

Is this running the entire file from scratch each time? If so, then it's not doing the same as a REPL, and values will change depending on other circumstances, like responses from APIs or REQUEST_TIME, etc.
You need to be careful about hosting this anywhere other than your local development box in case it turns out to be easily exploitable.

I'm curious as to why you'd decide to write a PHP app... in Python?

Collapse
datmt profile image
Mạnh Đạt Author

Thanks for your comment.

Well, maybe it is not a repl literally. What I wanted was something that evaluates the code I write instantly without executing co/reload the browser manually.

This project is for learning purposes only. It came from my own need actually. I need to strengthen my PHP knowledge so I write a lot of short snippets.

As per writing php app in python, well, it does what I need quickly (based on my current knowledge) so why not :)

Collapse
matthewbdaly profile image
Matthew Daly

Have you looked at PsySh? That's a really good REPL for PHP.

Collapse
datmt profile image
Mạnh Đạt Author

Yes, I tried it and it's truly a REPL. However, for long pieces of code, that isn't ideal. I should have titled it "hot reload"/"live reload" instead of "REPL" :)

Collapse
vlasales profile image
Vlastimil Pospichal

.vimconfig for PHP

setlocal makeprg=php\ %
map <F9> :lmake!