A couple of participants in Boston PHP‘s self-study PHP Percolate, Jim Wright and Jared Stenquist, are confused about the use of the -> operator, which is introduced on page 44 of PHP Solutions, 2nd Edition. Since others might be in a similar situation, I decided to write a blog post about it.
Although PHP isn’t 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’ve 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 wasn’t specified when creating the object, it’s 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 (they’re listed in Table 14-4 on page 402 of PHP Solutions, 2nd Edition) 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’s 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']
If you’re familiar with JavaScript, it should be obvious by now that the -> operator plays the same role in PHP as the dot operator in JavaScript. For example, in JavaScript, this produces the date for Christmas Day 2011:
var xmas2011 = new Date(2011, 11, 25); // months are zero-based
alert('Christmas 2011 is on ' + xmas2011.toString());
JavaScript also uses the dot operator for properties:
objectName.propertyName
Hopefully, that clarifies the role of the -> operator. If you still have questions, post them in the Comments section below.






