Page 206 - Beginning PHP 5.3
P. 206

Part II: Learning the Language
                 For example, the  Car  class might indicate merely that cars should have a color, whereas a specific  myCar
                object might be colored red.

                  The distinction between classes and objects is often confusing to those new to OOP. It helps to think of
                classes as something you create as you design your application, whereas objects are created and used
                when the application is actually run.

                  Properties

                   In OOP terminology, the characteristics of a class or object are known as its  properties . Properties
                 are much like regular variables, in that they have a name and a value (which can be of any type).
                 Some properties allow their value to be changed and others do not. For example, the   Car  class might
                have properties such as   color  and  weight . Although the color of the car can be changed by giving it
                 a new paint job, the weight of the car (without cargo or passengers) is a fixed value.

                  Methods

                   The behaviors of a class  —  that is, the actions associated with the class  —  are known as its  methods .
                 Methods are very similar to functions; in fact, you define methods in PHP using the   function  statement.

                  Like functions, some methods act on external data passed to them as arguments, but an object ’ s method
                can also access the properties of the object. For example, an   accelerate  method of the  Car  class might
                 check the   fuel  property to make sure it has enough fuel to move the car. The method might then update
                the object ’ s   velocity  property to reflect the fact that the car has accelerated.

                   The methods of a class, along with its properties, are collectively known as  members  of the class.



                  Creating Classes and Objects in PHP
                   Although the theory behind classes and objects can get quite involved, classes and objects are actually
                 really easy to create in PHP. As you ’ d imagine, you need to create a class before you create an object
                 belonging to that class. To create a class, you use PHP ’ s   class  keyword. Here ’ s a really simple class:
                    class Car {
                      // Nothing to see here; move along
                    }

                   This code simply defines a class called  Car  that does nothing whatsoever  —  it merely includes a
                comment. (You add some functionality to the class shortly.) Notice that a class definition consists of the
                  class  keyword, followed by the name of the class, followed by the code that makes up the class,
                 surrounded by curly brackets (  { } ).

                      A common coding standard is to begin a class name with a capital letter, though you don ’ t have to do
                    this. The main thing is to be consistent. You can find out more about coding standards in Chapter  20 .







              168





                                                                                                      9/21/09   9:03:31 AM
          c08.indd   168
          c08.indd   168                                                                              9/21/09   9:03:31 AM
   201   202   203   204   205   206   207   208   209   210   211