Node.js for PHP Developers by Daniel Howard

Node.js for PHP Developers by Daniel Howard

Author:Daniel Howard [Daniel Howard]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Web / Web Programming
ISBN: 9781449333799
Publisher: O'Reilly Media
Published: 2012-11-28T16:00:00+00:00


Other Variable Types

Objects, that is, PHP’s class-based system, can be converted to Node.js’s prototype-based system. The next chapter is devoted to converting both PHP classes to Node.js object with prototypes and PHP’s use of objects into the equivalent Node.js code that will use the corresponding Node.js objects.

PHP also has a two rather obscure variable types: resources and unknown types.

A PHP resource variable is an opaque data structure, also called a handle, whose data is only accessible by PHP API functions. For example, the PHP fopen() API function returns a resource value that points to a particular file. That resource value is stored in a variable and passed to other PHP API functions, such as fread(), fwrite(), and fclose(), which know how to understand the resource variables to read, write, and close the file, respectively. With resource variables, converting the variable itself from PHP to Node.js is not important; it is only important that the effect, such as reading, writing, or closing the file, takes place. Node.js has its own APIs for reading, writing, and closing files, so those are used instead. Converting 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 decompressing 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 Node.js API function calls.

An “unknown type” PHP variable is very obscure and may be a PHP resource variable that has been closed and rendered unusable. A PHP “unknown type” variable does not need to be converted to Node.js.

Node.js also has two variable types that have no corresponding types in PHP: function and undefined.

A Node.js function variable is a callback variable. The following Node.js code demonstrates that the f variable is a variable of type “function”:

var f = function() { }; console.log('The variable, f, is of type, "'+(typeof f)+'".');

The output of the Node.js code shows that the f variable is of a unique function type.

As you may recall from Chapter 4, PHP 5.3 introduced callback variables to the PHP language. Here’s a PHP 5.3 example of the previous Node.js code:

$f = function() { }; print 'The variable, $f, is of type, "'.(gettype($f)).'".';

The output of the PHP 5.3 example shows that PHP does not have a unique variable type for callback functions.

The variable $f is of type“object.”

PHP 5.3 callback functions are considered objects, along with other object variables that are not callback function variables. Node.js makes a distinction between functions and objects but PHP 5.3 does not.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.