Chapter 12. Miscellaneous Functions

Throughout the book, a common quick-and-dirty solution to porting certain PHP code to Node.js has been to implement PHP API functions as Node.js standalone functions. Since function calls in PHP and Node.js use the same syntax, no changes need to be made to the PHP calling code to call the new Node.js function; the previous PHP function call now becomes a Node.js function call simply by copying and pasting some Node.js code into the source file. While additional PHP to Node.js conversion will be needed, implementing PHP API functions as Node.js standalone functions can speed along PHP to Node.js conversion by resolving a large group of Node.js errors temporarily and leaving the developer to focus on the much fewer, remaining conversion issues.

It is astonishing how heavily the PHP language relies on PHP API functions, even though this is a long-running tradition that dates back to the C language. In contrast, Node.js relies heavily on object properties. Converting from PHP to Node.js is a much easier task than converting Node.js to PHP (if that was ever desirable) because emulating a Node.js property or method in PHP is a much more involved task than simply creating a Node.js function to emulate a PHP function. One way that PHP is superior to Node.js is how easily the underlying foundation of PHP API functions can be moved to a different language with much fewer changes needed to the PHP code itself.

For example, the following PHP code calls the ...

Get Node.js for PHP Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.