4.1 The Concepts of Object Orientation


In contrast to procedural languages, object-oriented languages treat all pieces of data that functions are working with as objects. When using the strict object-oriented paradigm, functions are no longer called functions but are called methods. Methods can only be defined for objects. A method for computing the sum of two values that has been defined for the object called a cannot be used for computing the sum of two values belonging to the object called b. At first glance, this seems stupid because you need two methods for performing one simple calculation, but in fact it can be an advantage. All methods belonging to a certain object can be treated as one union. Every object has a predefined programming interface, but the way things are treated by the object internally should not affect the person using that object. The advantage of this concept is that the user only has to be familiar with the interface and does not have to waste his time understanding things he doesn't need to know. If the programming interface of an object stays the same, the way things are done by the object internally can easily be changed without affecting the applications using that object.

Objects are also called classes. A class contains the definition of an object as well as all functions related to that object. Every class must have a constructor, which is used to create a new instance of an object. An instance of an object is an object created by a constructor. Imagine a class called people, which defines persons. Therefore a specific person is an instance of the class people. A class is simply a template, and all instances of an object have the same scheme as the class defining how an instance has to look.

4.1.1 Constructors and Destructors

To generate new instances of an object, a constructor has to be implemented into the object. Every time the constructor is called, an instance of the object is returned.

The constructor assigns all properties of an object to the variables defining an instance.

The counterpart of constructors is destructors. Destructors are used to destroy objects and to free the memory allocated by an object.

4.1.2 Inheritance

Object-oriented languages support inheritance. This means that child classes can inherit functions and attributes from parent classes. Assume a class for storing geometric objects. All geometric objects have attributes, such as color, in common. Circles, however, have additional attributes, such as the radius. Therefore it is possible that the class circle inherits all methods and functions from the parent class used to store all attributes that geometric objects have in common.

The ability to inherit methods and functions helps the programmer to keep code short because functions needed by many classes that are related to each other only have to be implemented once. One disadvantage of this feature is that applications that make heavy use of inheritance are difficult to debug because the function executed has to be found in the object tree. In this case, the procedural paradigm is easier to understand: In most cases, it is clear which function has been executed because there is only one with a certain name.

4.1.3 Function Overloading

Function overloading is another feature of object-oriented languages. Function overloading means that one function is implemented more than once. Every implementation of the function accepts different arguments passed to it. Imagine a function called sum that is used to compute the sum of two values. An additional function that is called sum as well is used to compute the sum of three values. Both functions have the same name but one function accepts two parameters, whereas the second function accepts three parameters.

PHP does not support explicit function overloading because there is an easy way to get around it by passing arrays to a function.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net