Data and Logic Together: Object Basics in PHP

Example 6-1 defines an Entree class to represent an entree.

Example 6-1. Defining a class

class Entree {

public $name;

public $ingredients = array();

public function hasIngredient($ingredient) {

return in_array($ingredient, $this->ingredients);

}

}

In Example 6-1, the class definition starts with the special keyword class followed by the name we’re giving to the class. After the class name, everything between the curly braces is the definition of the class—the properties and methods of the class. This class has two properties ($name and $ingredients) and one method (hasIngredientQ). The public keyword tells the PHP engine which parts of your program are allowed to access the particular property or method the keyword is attached to. We’ll get into that later, in “Property and Method Visibility” on page 113.

The hasIngredientQ method looks mostly like a regular function definition, but its body contains something new: $this. This is a special variable that refers to whatever instance of a class is calling the function. Example 6-2 shows this in action with two different instances.

Example 6-2. Creating and using objects

// Create an instance and assign it to $soup

$soup = new Entree;

// Set $soup’s properties

$soup->name = ‘Chicken Soup’;

$soup->ingredients = array(‘chicken’, ‘water’);

// Create a separate instance and assign it to $sandwich

$sandwich = new Entree;

// Set $sandwich’s properties

$sandwich->name = ‘Chicken Sandwich’;

$sandwich->ingredients = array(‘chicken’, ‘bread’);

foreach ([‘chicken’,’lemon’,’bread’,’water’] as $ing) {

if ($soup->hasIngredient($ing)) {

print “Soup contains $ing.\n”;

}

if ($sandwich->hasIngredient($ing)) {

print “Sandwich contains $ing.\n”;

}

}

The new operator returns a new Entree object, so in Example 6-2, $soup and $sandwich each refer to different instances of the Entree class.

The arrow operator (->), composed of a hyphen and a greater-than sign, is your road to the properties (variables) and methods (functions) inside an object. To access a property, put the arrow after the object’s name and put the property after the arrow. To call a method, put the method name after the arrow, followed by the parentheses that indicate a function call.

Note that the arrow operator used to access properties and methods is different from the operator that separates array keys and values in array() or foreach(). The array arrow has an equals sign: =>. The object arrow has a hyphen: ->.

Assigning a value to a property works just like assigning a value to any other variable, but with the arrow syntax to indicate the property name. The expression $soup->name means “the name property inside the object instance that the $soup vari­able holds,” and the expression $sandwich->ingredients means “the ingredients property inside the object instance that the $sandwich variable holds.”

Inside the foreach() loop, each object’s hasIngredient() method gets called. The method is passed the name of an ingredient, and it returns whether or not that ingre­dient is in the object’s ingredient list. Here you can see how the special $this variable works. When $soup->hasIngredient() is called, $this refers to $soup inside the body of hasIngredient(). When $sandwich->hasIngredient() is called, $this refers to $sandwich. The $this variable doesn’t always refer to the same object instance, but instead refers to the instance the method is being called on. This means that when Example 6-2 runs, it prints:

Soup contains chicken.

Sandwich contains chicken.

Sandwich contains bread.

Soup contains water.

In Example 6-2, when $ing is chicken, then both $soup->hasIngredient($ing) and $sandwich->hasIngredient($ing) return true. Both objects’ $ingredients properties contain an element with the value chicken. But only $soup->ingredients has water and only $sandwich->ingredients has bread. Neither object has lemon in its $ingredients property.

Classes can also contain static methods. These methods cannot use the $this variable since they do not get run in the context of a specific object instance, but on the class itself. Static methods are useful for behavior that is relevant to what the class is for, but not to any one object. Example 6-3 adds a static method to Entree that returns a list of possible entree sizes.

Example 6-3. Defining a static method

class Entree {

public $name;

public $ingredients = array();

public function hasIngredient($ingredient) {

return in_array($ingredient, $this->ingredients);

}

public static function getSizes() {

return array(‘small’,’medium’,’large’);

}

}

The declaration of the static method in Example 6-3 is similar to other method defi­nitions, with the addition of the static keyword before function. To call a static method, you put :: between the class name and the method name instead of ->, as shown in Example 6-4.

Example 6-4. Calling a static method

$sizes = Entree::getSizes();

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *