Skip to main content

Class and Object in PHP

 In PHP, a class serves as the blueprint for creating objects. It defines the properties (data) and methods (functions) that objects of that class will have. Here's a closer look at classes in PHP:

Defining a Class:

  • Classes are defined using the class keyword followed by the class name.
  • The class name should be capitalized and follow camelCase convention.
  • Within the curly braces {}, you define the class's properties and methods.

OOP Case

Let's assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.

When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

PHP - What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:

Class: Fruit

Object: Apple , Banana , Mango

Class: Car

Object: Volvo, Audi, Toyota 

So, a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Look at the next chapters to learn more about OOP.

Define a Class

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:

Syntax

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

Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting 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;
  }
}
?>

Define Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Objects of a class are created using the new keyword.

In the example below, $apple and $banana are instances of the class Fruit:

<?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 more methods to class Fruit, for setting and getting 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();
?>

PHP - The $this Keyword

The $this keyword refers to the current object, and is only available inside methods.

Look at the following 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 class (by adding a set_name() method and use $this):

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

echo $apple->name;
?>


Outside the class (by directly changing the property value):

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

echo $apple->name;
?>


PHP - instanceof

You can use the instanceof keyword to check if an object belongs to a specific class:

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

Example:

class Person { public $name; // Public property private $age; // Private property public function greet() { echo "Hello, my name is " . $this->name . "!"; } public function setAge($age) { if ($age >= 0) { $this->age = $age; } else { echo "Invalid age!"; } } public function getAge() { return $this->age; } }

Key Points:

  • Properties: Define the data associated with an object. They can be public (accessible from anywhere), private (accessible only within the class), or protected (accessible within the class and subclasses).
  • Methods: Define the behavior of an object. They can perform actions on the object's properties or interact with other objects.
  • Constructor and Destructor: Special methods that handle object creation and destruction, respectively.
  • Visibility: Use access modifiers like publicprivate, and protected to control access to properties and methods.

Creating Objects:

  • Use the new keyword followed by the class name to create an object.
  • You can then access the object's properties and methods using the object variable.

Example:

$person1 = new Person();
$person1->name = "John";
$person1->greet(); // Outputs: Hello, my name is John!

$person2 = new Person();
$person2->name = "Alice";
$person2->setAge(25);
echo "Alice's age: " . $person2->getAge(); // Outputs: Alice's age: 25

Remember, this is just a basic overview. OOP in PHP is a broad topic with various aspects like inheritance, polymorphism, and more. Feel free to ask further questions or specify areas you'd like to explore deeper!

Comments