https://github.com/miyako/4d-class-php
Class to use external PHP interpreter in execute, interactive or CGI mode
https://github.com/miyako/4d-class-php
4d-class php system-worker
Last synced: over 1 year ago
JSON representation
Class to use external PHP interpreter in execute, interactive or CGI mode
- Host: GitHub
- URL: https://github.com/miyako/4d-class-php
- Owner: miyako
- License: mit
- Created: 2024-01-26T04:52:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T07:39:56.000Z (almost 2 years ago)
- Last Synced: 2025-01-08T17:55:20.087Z (over 1 year ago)
- Topics: 4d-class, php, system-worker
- Language: 4D
- Homepage: https://miyako.github.io/4d-class-php/
- Size: 52.7 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README


[](LICENSE)

[Classes](https://github.com/miyako/4d-class-php/tree/main/Project/Sources/Classes) to use external PHP interpreter in execute, interactive or CGI mode.

# Introduction
The internal PHP Interpreter has been removed in v20 R3[^removed]. Now it is "strongly advised" to use `4D.SystemWoker` to execute PHP code.
To effectively replace the CGI-based `PHP Execute`[^phpexecute] with `4D.SystemWoker`[^systemworker] you need to be able to do 3 things:
1. Launch `php` once and keep it running
2. Talk to the interpreter through `stdIn` and hear back from it through `stdOut` and `stdErr`
3. Terminate the running `php` when you no longer need it
For 1. and 3. you need to keep track of the single instance of `4D.SystemWoker` that is bound to the PHP interpreter.
For 2. you need to implement a controller object and run your code in an execution context of a dialog or a worker. Ideally the controller would be a generic class that can be easily extended according to specific needs.
This project shares a set of classes to execute PHP from 4D by using an external interpeter and CGI (the `PHP Execute` command), interactive mode, or one-shot execute mode.
# Install
Get PHP from the offical site for Windows[^phpforwindows] or from my repository[^phpformac] for macOS. Alternatively, compile PHP from source. Place `php` and/or `php-cgi` at the following path:
```path
/RESOURCES/bin/{platform}/
```
# Examples
## Execute
```4d
$ini:=File("/RESOURCES/php/php.ini")
$PHP:=cs.PHP.new(cs._PHP_Controller; $ini)
$PHP.run($input)
//the data is returned synchronously
$stdOut:=$PHP.data
```
## Interactive
```4d
$ini:=File("/RESOURCES/php/php.ini")
$PHP:=cs.PHP.new(cs._PHP_Controller; $ini)
$PHP.interactive()
//you can now post messages and receive data asynchronously
$PHP.controller.worker.postMessage($input+"\r\n")
```
## CGI
```4d
$ini:=File("/RESOURCES/php/php.ini")
$CGI:=cs.PHP_CGI.new(cs._PHP_CGI_Controller; $ini).cgi()
//you can now use PHP Execute
```
[^removed]: [Deprecation of PHP Commands & Removal of 4D Built-in PHP Interpreter](https://blog.4d.com/deprecation-of-php-commands-removal-of-4d-built-in-php-interpreter/)
[^phpexecute]: [4D v20 R3 > 4D Language Reference > PHP > PHP Execute](https://doc.4d.com/4Dv20R3/4D/20-R3/PHP-Execute.301-6531696.en.html)
[^systemworker]: [Core Development > Class Functions > SystemWorker](https://developer.4d.com/docs/20-R3/API/SystemWorkerClass)
[^phpforwindows]: [PHP For Windows](https://windows.php.net)
[^phpformac]: [miyako/4d-topic-php](https://github.com/miyako/4d-topic-php/releases)