Web programming

Object-Oriented Programming (OOP) in PHP

Ngoc Phuong

Ngoc Phuong

2021-03-01 . 76 view

Extremely important knowledge when learning programming is Object-Oriented Programming (OOP), if you do not grasp this knowledge, then read this article below now!

I. Understanding Object-Oriented Programming OOP?

1. What is an object?

Objects are entities or entities with two common characteristics: state and behavior.

Example

  • A dog is an object with a state (name, color, breed, hunger or fullness status) and behavior (barks, fetch items, wags tail).
  • A bicycle is an object also having state (gears, current pedaling pace, current speed) and behavior (changing gears, adjusting pedaling rhythm, using brakes).
  • A student is an object with state (name, age, gender) and behavior (enter grades, pay tuition ... )

2. What is Object-Oriented Programming OOP?

OOP is the abbreviation for Object-Oriented Programming.

Object-Oriented Programming OOP is a programming method with new commands, new syntax and a new way of thinking in programming. With the traditional procedural programming method, we will write procedures or functions to perform operations on data, while object-oriented programming is creating objects that contain both data and functions.

3. Advantages of Object Oriented Programming OOP

Object Oriented Programming OOP makes execution faster and easier.

Object-oriented programming OOP provides a clear structure for programs.

Object-oriented programming OOP helps to avoid repeated code snippets and makes it easier to maintain, modify and debug.

Object oriented programming OOP enables you to create fully reusable applications with less code and shorter development time.

4. Characteristics in Object-Oriented Programming OOP

a. There are 2 important characteristics in Object-oriented OOP

  • The characteristic of reuse, reuse of source code (source reusability) expressed through the inheritance mechanism.
  • The characteristic of information hiding (data hiding) is expressed through class definition.

b. There are 4 important properties in Object-Oriented OOP

  • Abstraction: Selects the properties and methods of the object needed to solve the problem being programmed
  • Inheritance: A class can be defined based on another class and can inherit the properties and methods of the other class, take advantage of the source code, do not have to define it again, reuse the source code optimally.
  • Encapsulation: The attributes and methods of the object needed to solve the problem have been selected will be encapsulated into a data type called class. Aim to hide information from external objects, the attributes then the methods.
  • Polymorphism: A derivative class inherits a base class, at the derivative classes, methods can be rewritten to perform individual actions suitable for derivative classes.

5. Basic concept in Object-Oriented Programming OOP

a. Class

A class is a template for objects and an object is an instance of a class. This means that when an object is created, it inherits the attributes and methods of the class but each object will have different information values.

Classes are defined using the class keyword, followed by the class name and a pair of {}.

Syntax to create a Class

class Name_Class {
  // Your code is in here
}

For example, we create a class named Fruit which includes two properties ($name and $color) and two methods set_name() and get_name().

class Fruit {
  //Properties
  public $name;
  public $color;

  // Method
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}

Note: In a Class, variables are called attributes and functions are called methods!

b. Object

The class wouldn't make sense without an object.

We can create many objects from a class. Each object will have all the attributes and methods defined in the class, but they will have different attribute values.

To initialize an object we use the new keyword

For example, we initialize 2 objects to inherit the above Fruit class as follows

c. $this Variable

The $this variable is used to point to the current object and is only available within the methods. Meaning that from a method in the Class you want to access the properties, methods in that Class then you use the $this variable.

For example, we have a Fruit class with a $name attribute, to change the $name attribute we will do what.

class Fruit {
  public $name;
}
$apple = new Fruit();

There are 2 ways to change the $name property in the above example

The first way we add the set_name () method and use the $this variable

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

The second way is to directly change the value of the $name property

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

d. instanceof function

To verify that an object belongs to a particular class, we use the instanceof function.

For example

class Fruit {
  // Properties
  public $name;
  public $color;

  // Method
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}

$apple = new Fruit();
var_dump($apple instanceof Fruit);

II. Constructor and Destructor method

In Object-Oriented Programming OOP, to make the use of objects easier, PHP provides some Magic Methods also known as special methods used to handle certain actions that occur in objects. The Constructor and Destructor methods are 2 special methods that are most commonly used in Object-Oriented Programming OOP.

1. Constructor method

The constructor method allows you to initialize the properties of the object when creating the object.

If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

Note: The construct function starts with 2 underscores __

For example, without using the __construct function, we have the following code

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 "
";
echo "Color: " . $apple->get_color();

And when using the __construct function, we have the following code.

class Fruit {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  function get_name() {
    return $this->name;
  }
  function get_color() {
    return $this->color;
  }
}

$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "
";
echo $apple->get_color();

In the above example, we have already initialized 2 properties $name and $color in the __construct function, we will not need to add 2 set_name() and set_color() methods, this helps the code to be more concise.

2. Destructor method

The destructor method is called when the object is destroyed or the script is stopped.

If you create a __destruct () function, PHP will automatically call this function at the end of the script.

Note: The destruct function starts with 2 underscores __

For example

class Fruit {
  public $name;
  public $color;

  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  function __destruct() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

$apple = new Fruit("Apple", "red");

// Return: The fruit is Apple and the color is red.

In the above example, we clearly see the __destruct function was automatically called at the end of the program.

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback