PHP Objects

The area in which PHP differs the most from JavaScript is when you want to create objects.

With JavaScript you define an object by basing it on another object and adding the extra functionality if any that the new object requires. JavaScript is a prototyping language so each object can be used as the prototype for creating more objects and all objects apart from the Object object itself which is one of the pre-defined JavaScript objects are derived from other already existing objects.

PHP is completely different in that it isn't a prototyping language, it is an object oriented language. Where an object oriented language differs from a prototyping language is that an object oriented language makes a distinction between those objects that the program is going to use and those that form the definitions upon which those objects are based. In an object oriented language such as PHP the definition objects are called classes and each class that you define can either be defined completely from scratch or it can inherit from other already defined classes. You cannot se a class directly in your code though. In order to use a class you must create an object whose type is that class and then your code can work with that object (which will have all of the properties and methods that its class defines).

The basic differences between a prototyping and object oriented language mean that with an object oriented language all of the functionality of a class needs to be defined before you start creating objects of that type and you cannot add additional functionality to the class and have pbjects that are already defined be able to access that additional functionality. The definition and usage steps are completely separated in an object oriented language (hence classes and objects) whereas a prototyping language allows you to add functionality to an object at any time and if added the right way it will then be avaialble to all objects derived from that object even if they have already been created.

Another difference in the way that objects work in PHP compared to JavaScript is that in PHP you can specifically define the properties and methods as being private, protected, or public. In JavaScript these words have been reserved so as to allow the concept to be added into JavaScript at some future time but currently the scope of properties and methods is determined by where and how you define them and making a property private to an object rather than to a specific method is a rather convoluted process.

As a result of these differences in the way objects work in PHP as compared to JavaScript some concepts that are easily done using JavaScript are more complex in PHP and many that are simple in PHP are more complex in JavaScript. Having JavaScript code more complex than it needs to be is the more common of these two as many people have learnt how to use object oriented programming and have attempted to find ways to duplicate that methodology using JavaScript when JavaScript is not really an object oritented language and so does not readily support those constructs. Fewer people start out learning how to use a prototyping language correctly and so the attempts to duplicate prototyping functionality in an object oriented language are rarer. In amny cases those who do already know JavaScript and no other language have seen its use of objects and gone ahead to learn object oriented programming only to find that JavaScript doesn't really work that way. If you have done that then you will find that PHP which is an object oriented language is far easier for you to apply what you have learnt about object oriented processing since PHP is in fact an object oriented language.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate