PHP OOP - Classes and Objects


A classroom is a model of objects, and an object is a model of a class.


OOP Case

Suppose we have a fruit class. The fruit can have properties such as name, color, weight, etc. We can define variables such as $ name, $ color, and weight of $ to hold the values ​​of these properties.

When individual items (apple, banana, etc.) are created, they benefit from all the structures and behaviors in the classroom, but each item will have different values ​​of properties.


Define a Class

The class is defined using a class keyword, followed by a class name and two twisted parentheses ({}). All of its features and methods fit inside the braces:


Syntax
<?php
class Fruit {
  // code goes here...
}
?>

Below we announce a class named Fruit that combines two properties ($ name and $ color) and two sets_name () and get_name () for setting and acquiring $ 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 the classroom, the variables are called structures and the functions are called methods!



Define Objects

Classes are nothing but things! We can build many things in the classroom. Each item has all the structures and methods described in the class, but will have different property values.

Classroom items were created using a new keyword.

In the example below, $ apple and $ banana are examples of Class Fruit:


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, add two alternatives to the Fruit class, setup and acquiring $ 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 keyword $ this refers to the current item, and is only available in internal modes.

Consider the following example:


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

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

1. Inside the classroom (by adding set_name () 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 classroom (by directly changing the value of the building):


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

echo $apple->name;
?>


PHP - instanceof

You can use an instanceof a keyword to check if an item belongs to a particular category:


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