OOPs in PHP: All You Need to Know About Fundamental Concepts of OOPs

According to Indeed.com, the demand for PHP developers has massively increased to 834% since January 2020, and, it is today the fastest-growing tech job across the industry, today. 

In this OOPs in PHP tutorial, you will be exploring the following topics:

  • What is PHP?
  • What are the Concepts of OOPs in PHP?
    • Class
    • Object
    • Inheritance
    • Constructor
    • Destructor
    • Polymorphism
  • Conclusion

Let's get started with the first concept.

What is PHP? 

/OOPsInPHP_1

PHP or Hypertext Preprocessor is a server-side scripting language embedded into HTML used for developing dynamic websites.

What Are the Concepts Of OOPs in PHP?

The basic concepts of Object-oriented programming concepts are:

  • Class
  • Object
  • Inheritance
  • Constructor
  • Destructor
  • Polymorphism
  • Full Stack Web Developer Course

    To become an expert in MEAN StackView Course
    Full Stack Web Developer Course

Class

It is a blueprint of an object that provides the initial values for the state. A class consists of both data and functions. Data and functions together are called objects.

Depicted below is the Syntax of a class in PHP language:

<?php

class Fruit {

  // code goes here...

}

?>

In the code below, you must define a class, followed by ‘Fruit’, which is the class name. Next, include a pair of curly braces ({}) and add the properties and methods within the braces.

<html>

<body>

<h1> The fruit program </h1>

<?php

class Fruit {

    // Properties

    public $name;

    public $color;  

    // Methods

    function set_name($name) {

      $this->name = $name;

    }

    function get_name() {

      return $this->name;

    }

  }

?>

</body>

</html>

As you can see on the screen, you declared a fruit class that contains two properties called $name and $color. Along with that, you have also added two methods: set_name() and get_name(). The purpose of these methods is to set and get the $name property.

OOPsInPHP_2

When you define a class, a variable (within the class) is called property, and function is called method.

Now, head over to the browser and type ‘localhost slash’, followed by the file name. Once you do that, you will get the following output.

Output-

OOPsInPHP_3

Moving forward, you will know learn about objects in PHP.

Objects

  • Objects are an instance of a class. A variable holds the data of a class. You must define a class once and subsequently create as many objects as needed in it.
  • For better clarity, look at this example - if ‘car’ is a class, Mercedes Benz and BMW are variables. 
  • However, classes are incomplete without objects. You can add as many objects as you want from a class. 
  • Every object has its properties and methods defined in the class, but those objects will have different property values.
  • It creates objects of a class using the new keyword.

In the example below, $Orange and $Strawberry are instances of the class ‘fruit’.

<html>

<body>

<h1> The fruit program </h1>

<?php

class Fruit {

    // Properties

    public $name;

    public $color;

    // Methods

    function set_name($name) {

      $this->name = $name;

    }

    function get_name() {

      return $this->name;

    }

  }

  $orange = new Fruit();

  $strawberry = new Fruit();

  $orange->set_name('Orange');

  $strawberry->set_name('Strawberry');

  echo $orange->get_name();

  echo "<br>";

  echo $strawberry->get_name();

  ?>

  </body>

</html>

The output for the above code is:

OOPsInPHP_4.

Constructor

  • It helps an individual to allow an object's properties, while object creation takes place.
  • A constructor allows an individual to initialize an object’s properties while the object is created.
  • Suppose you build a program using __construct() function, PHP will automatically call the -construct function while creating an object from a class.
  • Also, remember that when you write the construct function, make sure that it is prefixed with two underscores. 
  • Now, let's look at this example, where you will use a constructor function by calling the set_name() method. This set_name function is often used when you assign a value to the name attribute.

The code with the constructor function is below-

<?php

class Fruit {

  public $name;

  public $color;

  function __construct($name) {

    $this->name = $name; 

  }

  function get_name() {

    return $this->name;

  }

}

$apple = new Fruit("Strawberry");

echo $strawberry->get_name();

?>

The output for the code is below-

OOPsInPHP_5

Next up, is the Destructor function in PHP.

Stand Out From Your Peers this Appraisal Season

Start Learning With Our FREE CoursesEnroll Now
Stand Out From Your Peers this Appraisal Season

Destructor

  • This function is called to perform an operation when the object stops working or is exited.
  • Suppose you build a program __destruct() function, PHP will automatically call this function at the end of the script.
  • Just like a constructor, the destructor function begins with two underscores (__)
  • Now, let us look at the example of a __destruct() function that is automatically called at the end of the script.

An example code with the Destructor function can be seen below-

<?php

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("watermelon", "green");

?>

The output for the above code is-

OOPsInPHP_6

After the destruction function, you are going to explore Inheritance in detail.

Inheritance

  • It derives new classes or child classes from the parent class. 
  • However, inheritance can have its own set of properties and methods.
  • Inheritance has two classes: 
  • Parent class: A Parent class is otherwise called a base class, that is inherited from another class.
  • Child class: A child class is a subclass that inherits from another class. A child class can have a subclass and derived class.
  • It derives the child class from all the properties and methods from the subclass. Besides, it can have its own set of properties and methods.
  • An inherited class is defined by using the extend keyword.

You can see the example code for Inheritance below:

<html>

<body>

<h1> The fruit program </h1>

<?php

class Fruit {

  public $name;

  public $color;

  public function __construct($name, $color) {

    $this->name = $name;

    $this->color = $color; 

  }

  public function intro() {

    echo "A {$this->name} is a fruit  and the color of the fruit cherry is {$this->color}."; 

  }

}

// Cherry is inherited from Fruit

class Cherry extends Fruit {

  public function message() {

    echo "Is cherry a fruit or a berry? "; 

  }

}

$cherry = new Cherry("Cherry", "red");

$cherry->message();

$cherry->intro();

?>

</body>

</html>

  • It derives the cherry class from the ‘Fruit’ class.
  • This shows that the cherry class uses both the public $name and $color properties, along with the public methods that are construct() and intro().
  • The cherry class also has its own method called message().

The output for the above code is:

OOPsInPHP_7

Next up is the final concept, which is Polymorphism. 

Polymorphism

  • Polymorphism has many forms. 
  • It utilizes the same function for different purposes. It has a class with a variety of functions simultaneously sharing a common interface.
  • Inherited methods are overridden by redefining the methods in the derived class.
  • The __construct() and intro() methods in the cherry class, which is a child class, will override the methods in the parent class (Fruit).

Below is the code for the polymorphism concept:

<html>

<body>

<h1> The fruit program </h1>

<?php

class Fruit {

  public $name;

  public $color;

  public function __construct($name, $color) {

    $this->name = $name;

    $this->color = $color; 

  }

  public function intro() {

    echo "The fruit is {$this->name} and the color is {$this->color}."; 

  }

}

class Cherry extends Fruit {

  public $weight;

  public function __construct($name, $color, $weight) {

    $this->name = $name;

    $this->color = $color;

    $this->weight = $weight; 

  }

  public function intro() {

    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram."; 

  }

}

$cherry = new Cherry("Cherry", "bright pink", 20);

$cherry->intro();

?>

</body>

</html>

As you can see, you have got the output for the above code.

OOPsInPHP_8 

Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!

Conclusion

Congratulations, you have reached the conclusion of this tutorial titled ‘OOPs in PHP’. Here, you learned different concepts of OOPs in PHP. You also executed suitable programs for each OOPs concept and achieved the necessary output, as well. 

Now comes the important question. Are you planning to perhaps enhance your expertise and build a career in web development? In that case, Simplilearn’s Full Stack Web Developer - MEAN Stack Master's Program will prove to be an excellent certification program for you. This Master’s bootcamp certification program covers all the fundamental and advanced concepts of top web development languages and allows you to gain a work-ready understanding of them by way of multiple industry-relevant projects and use cases. Do explore the course to know more.

Do you have any questions for us? Please add them in the comments section of this tutorial, and our team of experts will answer them for you at the earliest. 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.