Consider a class named Fruit. A Fruit class can have properties such as name, color, and weight. We define variables like $name, $color, and $weight to store these property values.
When individual objects (e.g., apple, banana) are created from this class, they inherit all the properties and behaviors defined in the class, but each object will have unique values for these properties.
A class is defined using the class
keyword, followed by the class name and a pair of curly braces ({}
). All properties and methods of the class are included within these braces.
<?php class Fruit { // code goes here… } ?> |
Below, we declare a class named Fruit that includes two properties ($name and $color) and two methods, set_name() and get_name(), for setting and retrieving the $name property:
<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } } ?> |
Note: In a class, variables are referred to as properties, and functions are known as methods! |
Classes alone are not functional without objects! You can create multiple objects from a class, each possessing all the properties and methods defined in the class but with different property values.
Objects are instantiated from a class using the new
keyword.
In the example below, $apple
and $banana
are instances of the Fruit
class:
<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } } $apple = new Fruit(); $banana = new Fruit(); $apple->set_name(‘Apple’); $banana->set_name(‘Banana’); echo $apple->get_name(); echo “<br>”; echo $banana->get_name(); ?> |
In the example below, we add two additional methods to the Fruit
class for setting and retrieving the $color
property:
<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } function set_color($color) { $this->color = $color; } function get_color() { return $this->color; } } $apple = new Fruit(); $apple->set_name(‘Apple’); $apple->set_color(‘Red’); echo “Name: “ . $apple->get_name(); echo “<br>”; echo “Color: “ . $apple->get_color(); ?> |
The $this
keyword refers to the current object and is accessible only within methods.
Consider the following example:
<?php class Fruit { public $name; } $apple = new Fruit(); ?> |
So, where can we modify the value of the $name
property? There are two ways:
set_name()
method and using $this
):<?php class Fruit { public $name; function set_name($name) { $this->name = $name; } } $apple = new Fruit(); $apple->set_name(“Apple”); echo $apple->name; ?> |
2. Outside the class (by directly modifying the property value):
<?php class Fruit { public $name; } $apple = new Fruit(); $apple->name = “Apple”; echo $apple->name; ?> |
You can use the instanceof
keyword to determine if an object is an instance of a specific class:
<?php $apple = new Fruit(); var_dump($apple instanceof Fruit); ?> |