4d-class-php

Class to use external PHP interpreter in execute, interactive or CGI mode

View the Project on GitHub miyako/4d-class-php

version platform license downloads

Classes to use external PHP interpreter in execute, interactive or CGI mode.

Introduction

The internal PHP Interpreter has been removed in v20 R31. Now it is “strongly advised” to use 4D.SystemWoker to execute PHP code.

To effectively replace the CGI-based PHP Execute2 with 4D.SystemWoker3 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 Windows4 or from my repository5 for macOS. Alternatively, compile PHP from source. Place php and/or php-cgi at the following path:

/RESOURCES/bin/{platform}/

Examples

Execute

$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

$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

$ini:=File("/RESOURCES/php/php.ini")
$CGI:=cs.PHP_CGI.new(cs._PHP_CGI_Controller; $ini).cgi()
//you can now use PHP Execute