Node.js for PHP Developers: Porting PHP to Node.js

Node.js for PHP Developers: Porting PHP to Node.js

Daniel Howard

Language: English

Pages: 288

ISBN: 1449333605

Format: PDF / Kindle (mobi) / ePub


If you’re an experienced PHP developer, you already have a head start on learning how to write Node.js code. In this book, author Daniel Howard demonstrates the remarkable similarities between the two languages, and shows you how to port your entire PHP web application to Node.js. By comparing specific PHP features with their Node counterparts, you’ll also discover how to refactor and improve existing PHP 4 or PHP 5 source code.

At the end, you’ll have two fully functional codebases to provide to your users. You can update both of them simultaneously, using this book as a reference to important aspects of PHP and Node.js.

  • Use a simple Node.js framework to convert individual PHP pages to Node.js files
  • Refactor blocking PHP source code to non-blocking Node.js source code that uses callbacks
  • Convert PHP syntax, variables, and types to Node.js
  • Use a step-by-step technique for implementing PHP classes and class inheritance in Node.js
  • Convert PHP file handling APIs into their Node.js equivalents
  • Compare approaches for using a MySQL database in Node.js and PHP
  • Convert PHP code for JSON or XML APIs into Node.js code for npm packages

Getting Started with Raspberry Pi

Uncovering African Agency: Angola's Management of China's Credit Lines

MacRuby: The Definitive Guide: Ruby and Cocoa on OS X

Sustainable Infrastructure: The Guide to Green Engineering and Design

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

conversion recipe. When the conversion recipe is applied, all the remaining code is moved into the callback of the Node.js API call. But with nonlinear PHP code, the new Node.js code will also require the exact same code to remain outside of the callback as well so that it can service the code paths that do not use the nonblocking Node.js API. This will be clearer in subsequent examples. Nonlinear PHP code can occur when a blocking PHP API call is in an if statement block that has more statements

the PHP page does not take the raw HTTP response object and ma‐ nipulate it directly. There is no corresponding PHP object for the res variable that exists in Node.js. Instead, the PHP engine creates a default HTTP response before it executes the PHP page. Later, when the PHP page is being processed, the PHP page can make PHP API calls to replace any default setting with a new setting. If no changes are made, that is, if the PHP page ignores the HTTP response object, the default HTTP response

file access from PHP to Node.js is specifically handled in Chapter 9. Besides file access, other PHP resource variables are created when compressing or de‐ compressing files, using cURL, reading or writing to databases, reading or writing from FTP servers, creating images and documents in specific formats, reading or writing LDAP servers, and receiving or sending email. In all these cases, the resource variable itself can be ignored and the PHP API function calls replaced with the corresponding

Remove the ampersand (&), and if it still exists, the dollar sign ($) from the function parameters. For example, the f() function still uses the ampersand (&) for the a and c variables: function f(&a, b, &c) { ... } 154 | Chapter 7: Variables Remove the ampersand (&) from the a and c variables so all variables are passed by value: function f(a, b, c) { ... } Although PHP function parameters are not often passed by reference, it is useful to know how to convert them to Node.js. PHP functions

Writing Files The PHP file_get_contents() API function is one of the easiest and most common ways to read a file. It was added in PHP 4.3.0 and was immediately popular so only very old PHP 4 code does not use it. The following PHP code reads the file named data.txt from the same folder that the PHP file is in. The file is read as a long string that is assigned to the $contents variable: $contents = file_get_contents('data.txt'); print $contents; If a file named data.txt does not exist, a false

Download sample

Download