4D v16.3

Executing PHP scripts in 4D

Home

 
4D v16.3
PHP
Executing PHP scripts in 4D

Executing PHP scripts in 4D  


 

 

4D lets you directly execute PHP scripts. This new possibility gives you access to the wealth of utility libraries available via PHP. More particularly, these libraries provide the following functions:

  • ciphering (MD5) and hashing,
  • handling of ZIP files,
  • handling of pictures,
  • LDAP access,
  • COM access (control of MS Office documents), etc.

This list is not exhaustive. For a complete list of the PHP modules available by default with 4D, please refer to the PHP modules support section. Also, please note that it is possible to install additional custom modules.

To execute a PHP script or function, you must use the PHP Execute command. By default, 4D includes version 5.4.11 of PHP. Executed scripts must be compatible with this version and with the modules installed. 

For a complete description of PHP commands and syntax, please refer to the abundant PHP documentation available on the Internet. As an example, here are the addresses for a few reference sites:
http://us.php.net/manual/en/
http://phpdeveloper.org/
http://php.start4all.com/
http://php.resourceindex.com/Documentation/

4D provides a PHP interpreter compiled in FastCGI, client-server type communication protocol between an application and a PHP interpreter. 

The PHP interpreter pilots a set of system execution processes called "child processes". These processes are dedicated to the processing of requests sent by 4D. The execution of the requests is synchronous. For optimization reasons, by default up to five child processes can be run simultaneously (this number can be modified via the Database Settings or via the SET DATABASE PARAMETER command). Under Mac OS, these processes launched on the first request and are kept permanently by the PHP interpreter. Under Windows, 4D creates the processes according to its needs and recycles them if necessary. 4D automatically supports the handling of the processes of the PHP interpreter provided by default (launching and closing). 

Note: If the 4D program quits unexpectedly while child PHP processes are still active, you must delete them manually via the system process management window. 

The following diagram illustrates the 4D/PHP architecture of 4D:

This architecture works with a system of internal requests sent by 4D to a predefined TCP address (IP address and port number). If necessary, for example if several PHP interpreters are executed on the same machine, this address can be modified via the Database Settings or via the SET DATABASE PARAMETER command.

Warning: If you launch two 4D applications on the same machine and execute PHP statements on each of them (via the PHP Execute command), it is imperative to modify the listening ports of the FastCGI PHP interpreter so that you use a different one for each application. Otherwise, PHP statements may fail to execute correctly in an unpredictable manner and even freeze your 4D application.

You can choose to use another PHP interpreter than the one provided by 4D. This allows you to keep the same PHP interpreter even if 4D is updated. In addition, it allows you to install all the custom modules you want -- in fact, you cannot use a custom PHP file with the interpreter included in 4D. To use PHP configuration options other than the ones provided by default, you need to manage an external interpreter.

A custom PHP interpreter must respect two conditions:

  • It must be compiled in FastCGI,
  • It must be located on the same machine as 4D.

To use a custom PHP interpreter, you simply need to configure it so that it listens to a specific address and TCP port and to indicate to 4D to not activate the internal interpreter. These parameters can be specified either via database settings, or for the session via the SET DATABASE PARAMETER command. Of course, in this case, you must manage the starting and functioning of the interpreter yourself. 

The php.ini initialization file is placed in the Resources folder of the database. The php.ini file can be used, more particularly, to declare the location of the PHP extensions.
If this file is not present during the first call, 4D will create it with the appropriate configuration options.

The php.ini file of the external interpreter must contain the following entries:

  • auto_prepend_file which provides the complete pathname to the 4D_Execute_PHP.php utility script. This script is found in [4D application]Resources/php/Windows or /Mac. Without this entry, only complete scripts can be executed: calls to a routine inside a script will not work.
  • display_errors sets a "stderr" so that 4D can be informed when an error occurs during the execution of PHP code. Example:
    ; stderr - Display the errors to STDERR (only affects the CGI/CLI)
    ; To direct the errors to STDERR for the CGI/CLI:
    display_errors = "stderr"
For more information about the configuration of custom php.ini files, please consult the comments found in the php.ini file provided by 4D.



See also 

PHP Execute

 
PROPERTIES 

Product: 4D
Theme: PHP

 
HISTORY 

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)