PHP OOP - Classes and Objects

OOP Case

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

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.

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:

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

    In a class, variables are called properties and functions are called methods!

    `
  •       
                            <?php
    class Fruit {
        // Properties
        public $name;
        public $color;
    
        // Methods
        function set_name($name) {
        $this->name = $name;
        }
        function get_name() {
        return $this->name;
        }
    }
    ?>
                        

    In the example below, we add two more methods to class Fruit, for setting and getting the $color property:

    Objects of a class is 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 "
    "; 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 "
    "; 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:

    • 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");
    ?>
                        

    • Outside the class (by directly changing the property value):
    `
  •       
                            <?php
    function addNumbers(int $a, int $b) {
        return $a + $b;
    }
    echo addNumbers(5, "5 days");
    // since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
    ?>                                                 
                 
                        
  •       
                             
    <?php
    class Fruit {
        public $name;
    }
    $apple = new Fruit();
    $apple->name = "Apple";
    ?>
                 
                        

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

    Dess App

    DessApp is an Integrated E-learning Education, Interactive and User-friendly features, smarter options and redefining your school costs effectively and efficiently.

    View
    1 1