All articles

Build reusable PHP components

Create a PHP Class and Call Its Method on Two Objects

Build a PHP Greeting class, create two objects, and check why the same method greets Ada and Lin separately.

Editorial illustration for Create a PHP Class and Call Methods on Two Objects

To create a named PHP class, put its properties and methods inside a class definition, then use new to make an object from it. Use -> to access that object's property or method. With two objects, you can call the same method twice and have it read a different name each time. The key is $this: inside the method, it represents the object receiving that particular call.

Define a named class with a property and a method

A class definition starts with the class keyword and a name. Its braces contain the properties and methods belonging to that class, as described in the PHP manual. A property is a variable in a class; a method is a function defined in a class. Here, name is the property and sayHello() is the method. Unlike a standalone function called by name, this method is called through an object, which gives $this->name a particular property to read.

This example includes the opening PHP tag, the class definition, and the code that uses it:

<?php
class Greeting
{
    public $name;

    public function sayHello()
    {
        return "Hello, " . $this->name . "!";
    }
}

$first = new Greeting();
$second = new Greeting();

$first->name = "Ada";
$second->name = "Lin";

echo $first->sayHello() . "\n";
echo $second->sayHello() . "\n";

Expected output:

Hello, Ada!
Hello, Lin!

Inside sayHello(), $this refers to the object on which the method was called. That is why the method reads $this->name rather than naming $first or $second. Neither person's name needs to be written into the method definition. Keeping the method free of either object's variable name lets the same definition serve both calls.

Create two objects with new and set their property values

Each new Greeting() creates an instance of the Greeting class. The example keeps those instances in $first and $second so it can work with them separately. It then uses ->name to assign "Ada" to the first object's property and "Lin" to the second object's property. The public declaration lets the example access name from outside the class.

Notice where the values are assigned: after the two objects are created, not inside the class definition. The class supplies the property and method that both objects can use. There is no need to define a second class just to greet a second person. If you change only $first->name, the assignment to $second->name still says "Lin".

Check which object supplies each result

The example calls sayHello() once through $first and once through $second. To follow those calls, start with the object before ->sayHello(). In $first->sayHello(), $this is the calling object, $first, so $this->name reads "Ada". The method builds Hello, Ada!, and the first echo adds a newline. In $second->sayHello(), $this is $second, so the same expression reads "Lin". The second echo therefore places Hello, Lin! on the next line. This follows the manual's definition of $this as the value of the calling object.

If both greetings appear to use the same name after you edit the example, inspect both sides of the process: which object receives each ->name assignment, and which object appears before ->sayHello() in each call. Those are separate choices. Assigning Ada to $first would not make the first greeting use Ada if you then called the method through $second twice.

For a small check, change only $first->name = "Ada"; to another name. The first greeting should reflect that edit, while the second should still say Hello, Lin!. The method itself needs no change: $this selects the property of whichever object receives each call.

← All articles