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

Unit 8: Objects



            8.4 Accessing Properties and Methods                                                  Notes

            PHP is not an object-oriented language; it has extensive support for objects. Also, since PHP
            5, many core aspects of the language use objects rather than ordinary (procedural) functions.
            An object is a special data type that is capable of storing and manipulating values. You create
            an object from a class, which is a collection of functions and variables that define the object’s
            characteristics. Some classes, such as DateTime and Mysqli, are predefined by PHP, but you
            can also define your own.

            The advantage of using classes and objects is that, once the class has been defined, they reduce
            the amount of code you need to write. An object inherits all the functions and variables defined
            by the class. That’s not all. Each object is independent, so you can create several objects from
            the same class to store different values, but they all share the same functions. Up to now, I have
            referred to functions and variables, but when talking about objects and classes, a function is
            called a method, and a variable is called a property. Whenever you want to use an object’s method
            or property, you need to use the -> operator.
            You create an object by calling the class’s constructor method (which has the same name as the
            class) with the new keyword. Most constructor methods also accept arguments that set the initial
            properties of the object. In the case of the built-in DateTime class, you can use a string to specify
            the date. Without any arguments, it creates an object for the current date and time. For example,
            the following code creates two objects, one for today, and the other for Christmas Day 2011:

            $today = new DateTime(); $xmas2011 = new DateTime(‘12/25/2011’);
            The $today object now contains the current date and time, but $xmas2011 contains the date
            for December 25, 2011 (because the time was not specified when creating the object, it is set to
            midnight at the start of the day).

            To display the day of the week, you need to use the DateTime class’s format()method, and pass
            it a format string  for the weekday name like this:

            echo $today->format(‘l’);
            This displays whatever day it is today. However, the date stored in $xmas2011 is independent
            of $today. The following code displays “Sunday”:
            echo $xmas2011->format(‘l’);  // Sunday

            Using the -> operator is very similar to passing a variable as an argument to a function. Instead
            of putting the variable between the function’s parentheses, you attach the function (method) to
            the variable with the -> operator. What it actually means is “use the format() method with the
            value stored in this object ($xmas2011)”. In addition to format(), the DateTime class has other
            methods, such as setDate() and add(), that can be used to modify the date.
            Many objects also have properties that you can access. An object’s properties are similar to values
            stored in an array. The big difference is that the class definition can control how a property
            is accessed and modified by specifying whether it is public, protected, or private. Only public
            properties are visible and can be modified outside the class definition; protected and private
            ones are hidden from view. You access a public property using the -> operator like this:
            $someObject->propertyName.

            This is the equivalent of accessing an array element like this:
            $arrayName[‘elementName’]




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