PHP Classes & Objects
In this page:
class ClassName {
public $property;
function method_name() { }
}
$object = new ClassName();
$object->property = value; // set a property
$object->method_name(); // call a method
Creating a Class
You define a class with the class keyword followed by a name and a body containing properties (variables) and methods (functions) that belong to it, like class Car { public $color; function drive() {} }.
Example: Creating a Class
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`
public $color;
// Define the function `drive` with no parameters
function drive() {
// Print "Driving!" to the output
echo "Driving!";
}
}
?>
Login to try C/C++/Java/PHP code in the editor
Instantiating Objects
You create an object from a class using the new keyword, such as $myCar = new Car();, which allocates a fresh instance with its own independent copy of the class's properties.
Example: Instantiating Objects
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`
public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
// Print a detailed dump (with types) of `$myCar`
var_dump($myCar);
?>
Login to try C/C++/Java/PHP code in the editor
Accessing and Modifying Properties
Object properties are accessed with the -> (arrow) operator, so $myCar->color = red; sets that specific object's color without affecting any other Car object you might have created.
Example: Accessing and Modifying Properties
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`
public $color;
}
// Create a new `Car` instance, stored in `$myCar`
$myCar = new Car();
$myCar->color = 'red';
// Print `$myCar->color` to the output
echo $myCar->color;
?>
Login to try C/C++/Java/PHP code in the editor
Writing Class Methods
You can create multiple independent objects from the same class, and changes to one object's properties never affect another object's properties, since each holds its own separate state in memory.
Example: Writing Class Methods
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`
public $color;
}
// Create a new `Car` instance, stored in `$car1`
$car1 = new Car();
$car1->color = "red";
// Create a new `Car` instance, stored in `$car2`
$car2 = new Car();
$car2->color = "blue";
// Print `$car1->color . " " . $car2->color` to the output
echo $car1->color . " " . $car2->color;
?>
Login to try C/C++/Java/PHP code in the editor
Using the $this Keyword
Methods defined inside a class can read and modify that object's own properties directly by name, without needing to pass them in as parameters, since they execute in the context of that specific object.
Example: Using the $this Keyword
<?php
// Define the class `Car`
class Car {
// Declare the public property `$color`, set to "red"
public $color = "red";
// Define the function `showColor` with no parameters
function showColor() {
// Print `$this->color` to the output
echo $this->color;
}
}
// Create a new `Car` instance, stored in `$car`
$car = new Car();
$car->showColor();
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting
newwhen creating an object, such as$p = Person();, which calls an undefined function. - Using
->to access a static member or::to access an instance member. - Writing
$nameinstead of$this->nameinside a method.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: