Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP OOP – Classes / Objects

OOP Case

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.

Define a Class

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.

Syntax

<?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!

Define Objects

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:

Example

<?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:

Example

<?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();
?>

PHP – The $this Keyword

The $this keyword refers to the current object and is accessible only within methods.

Consider the following example:

Example

<?php
class Fruit {
  public $name;
}
$apple = new Fruit();
?>

So, where can we modify the value of the $name property? There are two ways:

  1. Within the class (by adding a set_name() method and using $this):

Example

<?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):

Example

<?php
class Fruit {
  public $name;
}
$apple = new Fruit();
$apple->name = “Apple”;

echo $apple->name;
?>

PHP – instanceof

You can use the instanceof keyword to determine if an object is an instance of a specific class:

Example

<?php
$apple = new Fruit();
var_dump($apple instanceof Fruit);
?>