O'Reilly logo

Node.js for PHP Developers by Daniel Howard

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Classes

All languages, including PHP and Node.js, have a set of fundamental variable types, such as booleans, numbers, and strings. For PHP and Node.js, these fundamental types were described in detail in the previous chapter. Variables can be created of these fundamental types. Many languages, including PHP and Node.js, also allow variables and functions to be combined together into a single more complex variable.

PHP uses what is called a traditional class-based object system. In the PHP class-based object system, a PHP class is a type that is defined by the user. Like a fundamental type, such as a boolean or a number, a type is not an actual variable; it is a kind of variable. A PHP object is an actual variable that was created from a PHP class.

Node.js does not use a traditional class-based object system; it uses a prototype-based system. In a prototype-based system, objects share a common object, called a prototype, which can provide variables and functions to be used by all objects that use that prototype. A Node.js object is an actual variable. A Node.js prototype object, or just called a “prototype,” is a Node.js object shared by multiple Node.js variables.

Encapsulation

To learn how to convert a PHP class to Node.js, let’s begin with a simple PHP class.

The following PHP code creates a simple class called Format, which holds a format string, like Error #{$1}: {$2}. The format string contains literal text and macros. Macros are contained in curly braces ( { and } ) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required