Tech Tip: Using built in PHP functions with flags
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: April 8, 2012
Using PHP is a great way to expand the power of 4D. However, some of the built in commands require flags to control their operation. The flags are constants that are translated to integers by the PHP interpreter. This causes a problem when trying to execute the function using PHP Execute. The PHP function is expecting an integer but the integer values that corrospond to the constants aren't usually listed in the documentation. Consider the following PHP command.file_put_contents($file, $person, FILE_APPEND);
To use this command in 4D with PHP Execute the third parameter FILE_APPEND must be changed to an integer. FILE_APPEND has a value of 8.
isOk:=PHP Execute("";"file_put_contents";*;$file;$person;8) |
This requires the value of the constant to be known and results in difficult to read code. A better way is to use the PHP function "constant", this function will return the value of a constant.
isOk:=PHP Execute("";"constant";$flag;"FILE_APPEND") isOk:=PHP Execute("";"file_put_contents";*;$file;$person;$flag) |
This method is easier to read and write. It is also more flexible than hard coding values.