Web Technologies

OOP in PHP

Here is the very basic Object Oriented Programing concept for PHP. Variables = properties = class variables in OOP & methods = functions

Object oriented programing is a method to write the code. It is easy to manage, easy to re use in compare to functional programing or procedural programing code.

Class is like blueprint of house and objects are real house. So for one blueprint there are many houses which are almost same in structure but different in interior or color etc.

Class is container of variables and methods. Variables & Methods can be Public, Private & Protected or Static.

There are some magic methods in class define with __

like: __construct(), __destruct(), __get(), __set()

We use constructors to assign class variable a values which we are going to pass when we create a new object of class.

Same destructors also auto called when no object reference to class anymore. It uses to clean up & end db connections etc.

class Fruit {
  public $name;
  function set_name($n) {  // a public function (default)
    $this->name = $n;
  }
}
$mango = new Fruit();
$mango->set_name('Mango'); // OK

Class can be inherited if not defined as FINAL.

STATIC class variables & methods can be called directly without Object.

echo firstClass::$mycountry;
childClass::toAll();

Also static variables can access inside the class using self::mycountry

Methods/functions can be either Public or Private or Protected and static also.

Private method can accessible to that class only & protected method can be accessible to that class as well as to child class only.

Inside the class to access class variable or methods, we use $this-> & outside the class we use Object->

__get() used to retrieve private properties outside the class. It uses if_property_exists then echo.

__set() used to set properties value outside the class. if uses if_property_exists then assign value.

Inside the child class, in constructor we have to call parent constructor using parent::__construct(). Please note if class inherit parent doesn’t mean parent constructor is auto called. You have to called it manually.

return statement stops function code immediately and scope back to the statement where we called the function. return doesn’t mean echo/print something.

<?php

echo 'This is oops concept.';
// Creating a very basic class & function and do sum of 2 fixed value numbers
class firstClass
{
    public $x;
    public $y;
    public function __construct()
    {
        $this->x = 10;
        $this->y = 20;
    }
    public function sumNum()
    {
        return $this->x + $this->y;
    }
}

// Calling function inside the class with private keyword
// Passing the arguments
$obj1 = new firstClass();
echo $obj1->sumNum();

class secondClass
{
    private $x;
    private $y;

    public function __construct($a, $b)
    {
        $this->x = $a;
        $this->y = $b;

        echo $this->sumNum();
    }

    private function sumNum()
    {
        return $this->x + $this->y;
    }
}

$obj2 = new secondClass(4, 56);
// $obj2->sumNum(); //This gives error that you try to access private function outside of class

// Try with protected and inheritance idea
// call a parent constructor
// call a parent function
// use of static variable outside of class using ::
// use of static variable inside the class using self keyword


class thirdClass
{
    protected $x;
    protected $y;

    protected $z;

    public function __construct($a, $b)
    {
        $this->x = $a;
        $this->y = $b;
        $this->z = $this->multiNum();

        echo "Parent class value $this->z<br>";
    }

    protected function multiNum()
    {
        return $this->x * $this->y;
    }
}
class fourthClass extends thirdClass
{
    private $num = 100;

    static $prop = "60 Lac";

    public function __construct($num1, $num2)
    {
        parent::__construct($num1, $num2);
        echo $this->num;
        // update $num with parent class variable
        // echo $this->z;
        $this->num = $this->z;
        echo $this->num;
        echo "My properties are of " . self::$prop;

        echo $this->x;
        echo $this->y;
        echo parent::multiNum();
    }
}

$obj3 = new thirdClass(20, 30);
// $obj3->multiNum(); //This gives error to access private fn
$obj4 = new fourthClass(3, 4);
echo $obj4::$prop;