Page 180 - Open Soource Technologies 304.indd
P. 180

Web Technologies-I



                   Notes         An object is an instance of a class. In this case, it is an actual user data structure with attached
                                 code. Objects and classes are a bit like values and data types. There’s only one integer data type,
                                 but there are many possible integers. Similarly, your program defines only one user class but
                                 can create many different (or identical) users from it.
                                 The data associated with an object are called its properties. The functions associated with an
                                 object are called its methods. When you define a class, you define the names of its properties
                                 and give the code for its methods.

                                 Debugging and maintenance of programs is much easier if you use encapsulation. This is the
                                 idea that a class provides certain methods (the interface) to the code that uses its objects, so the
                                 outside code does not directly access the data structures of those objects. Debugging is thus
                                 easier because you know where to look for bugs. The only code that changes an object’s data
                                 structures is in the class—and maintenance is easier because you can swap out implementations of
                                 a class without changing the code that uses the class, as long as you maintain the same interface.
                                 Any nontrivial object-oriented design probably involves inheritance. This is a way of defining a
                                 new class by saying that it is like an existing class, but with certain new or changed properties
                                 and methods. The old class is called the super class (or base class), and the new class is called
                                 the subclass (or derived class). Inheritance is a form of code reuse—the base-class code is reused
                                 instead of being copied and pasted into the new class. Any improvements or modifications to
                                 the base class are automatically passed on to the derived class.

                                 8.3 Creating an Object


                                 It is much easier to create objects and use them than it is to define object classes, so before we
                                 discuss how to define classes, let’s look at creating objects. To create an object of a given class,
                                 use the new keyword:

                                 $object = new Class;
                                 Assuming that a Person class has been defined, here’s how to create a Person object:
                                 $rasmus = new Person;

                                 Do not quote the class name, or you will get a compilation error:
                                 $rasmus = new ‘Person’; // does not work
                                 Some classes permit you to pass arguments to the new call. The class’s documentation should
                                 say whether it accepts arguments. If it does, you will create objects like this:
                                 $object = new Person(‘Fred’, 35);

                                 The class name does not have to be hardcoded into your program. You can supply the class
                                 name through a variable:

                                 $class = ‘Person’; $object = new $class; // is equivalent to $object = new Person;
                                 Specifying a class that does not exist causes a runtime error.
                                 Variables containing object references are just normal variables—they can be used in the same
                                 ways as other variables. Of particular note is that variable variables work with objects, as shown
                                 here:
                                 $account = new Account; $object = ‘account’ ${$object}->init(50000, 1.10); // same as $account-
                                 >init




        174                               LOVELY PROFESSIONAL UNIVERSITY
   175   176   177   178   179   180   181   182   183   184   185