DEV Community

Tanzim Ibthesam
Tanzim Ibthesam

Posted on • Edited on

OOP Php part-1

By OOP we mean Object Oriented Programming. In procedural programming we write procedures that perform operations on the data but in OOP we create classes and objects that performs operations on data.
1.Class,Object,methods,constructor
Class is blue printing of an object whatever we see around can be termed as an object.A car a student or an animal can be sited as examples.

<?php 
 class Student{
    //class is blue print of an object.Wtever we see around can be termed as an object
    //properties
    public $name;
    public $age;
}

?>
Enter fullscreen mode Exit fullscreen mode

This is how we write class in PHP.
Properties are features or characteristics of a class. If we want to assign a value or have access to any of the properties inside the object we need to first instantiate the class.Here we have defined variables according to the properties $name,$age.

Instantiate the class

//Instantiate the class
$stu=new Student();
//assign value to a property 
echo $stu->name="Tanzim";
echo $stu->age = 25;

Enter fullscreen mode Exit fullscreen mode

Here $stu variable is used to instantiate the new class. With the help of $stu variable I can assign values to any of the properties in the class.
If we want to print all in a single line we can write

echo "His name is  $stu->name and his age is  $stu->age";

Enter fullscreen mode Exit fullscreen mode

But this is surely not a good practice. As this code is not reusable in future if we want to instantiate this class more times like its $stu if we want to create $stutwo $stuthree. We need echo the same line every time with $stutwo->name and age is $stutwo->age. As a result it will lengthen our code.
Here we are instantiating the class student again with variable $stutwo

//Instantiate the class
$stutwo=new Student();
//assign value to a property 
echo $stutwo->name="Tanzim";
echo $stutwo->age = 25;
echo "His name is  $stutwo->name and his age is  $stutwo->age";

Enter fullscreen mode Exit fullscreen mode

Methods
They are functions inside the class through which we can have access to properties inside the class

class Student{
    //class is blue print of an object.Wtever we see around can be termed as an object
    //
    public  $name;
    public  $age;
    //methods
    public function profile()
    {
        # code...
        echo "His name is {$this->name} and his age is {$this->age}";
    }
}


Enter fullscreen mode Exit fullscreen mode

We need to use $this keyword to access any property inside of the method inside the class

$stu=new Student();
//assign value to a property 
echo $stu->name="Tanzim";
echo $stu->age = 25;
echo "<br>";

//We can want to print all properties ina place we can do 

echo "His name is  $stu->name and his age is  $stu->age";

$stu->profile();
Enter fullscreen mode Exit fullscreen mode

Through $stu->profile() we can easily have access to the properties If we instantiate another student which is stutwo

class Student{
    //class is blue print of an object.Wtever we see around can be termed as an object
    //
    public  $name;
    public  $age;
    //methods
    public function profile()
    {
        # code...
        echo "His name is {$this->name} and his age is {$this->age}";
    }
}

//Instantiate the class
$stu=new Student();
//assign value to a property 
echo $stu->name="Tanzim";
echo $stu->age = 25;
echo "<br>";

//We can want to print all properties ina place we can do 

echo "His name is  $stu->name and his age is  $stu->age","<br>";
$stu->profile();
//Instantiate another class with 
$stutwo = new Student();
//assign value to a property 
echo $stutwo->name = "Robin","<br>";
echo $stutwo->age = 25, "<br>";
echo "<br>";

//We can want to print all properties ina place we can do 

// echo "His name is  $stu->name and his age is  $stu->age";
$stu->profile();
$stutwo->profile();

?>
Enter fullscreen mode Exit fullscreen mode

The output we get is
Alt Text
This is the output we get we see that when we instantiate the class with second object we get all properties of the class with object $stutwo. $stutwo->profile() we get access to the properties inside class.

This is good but everytime we need to assign a value with the help of the object every time we instantiate the class with a new object

$stu=new Student();
//assign value to a property 
echo $stu->name="Tanzim";
echo $stu->age = 25;
echo "<br>";

//We can want to print all properties ina place we can do 

echo "His name is  $stu->name and his age is  $stu->age","<br>";
$stu->profile();
$stutwo = new Student();
//assign value to a property 
echo $stutwo->name = "Robin","<br>";
echo $stutwo->age = 25, "<br>";
echo "<br>";

Enter fullscreen mode Exit fullscreen mode

Here we see the created the first object by instantiating the class Student $stu then we created another object $stutwo by instantiating the class Student again ;
In both cases we used the object $stu and $stutwo to assign values to the properties which are to be accessed by the method profile
This is nice but more efficient way is using constructor magic method.
2.Construct
Objects properties can be initialized just by creating an object.
Here upon creation of __construct() function PHP will call this function automatically when we instantiate the object.
Here we printed a value inside the magic method construct

<?php 
class Student{
    public $name;
    public $age;


    public function __construct(){
        echo "Hello World from contructor";
    }

    public function profile(){
        echo "His name is {$this->name} and age is {$this->age}";
    }
}
$stu=new Student();
$stu->name="Tanzim";
$stu->age=25;
$stu->profile();
?>
Enter fullscreen mode Exit fullscreen mode

Here we see just upon instantiating the class we get what we printed inside the construct class.
Alt Text
We can easily put our properties in here and assign parameter as variables

 public function __construct($name,$age)
    {
        $this->name=$name;
        $this->age=$age;
    }


Enter fullscreen mode Exit fullscreen mode

Now the benefit is we can easily pass arguments as values of properties to these parameters
Just upon instantiating the class we can write

$stu=new Student("Tanzim",25);
echo "<br>";
$stu->profile();
echo "<br>";
$stutwo = new Student("Robin", 25);
echo "<br>";
$stutwo->profile();

Enter fullscreen mode Exit fullscreen mode

We now see we instantiate the class two times created two objects and then we can pass arguments as values upon instantiation like in the above code shown
3.Inheritance
It is a way through one class inherits properties and methods from another class
At first we create a class

class Student{
    public $age;
    public $name;

    public function profile()
    {
        # code...
        echo "His name is {$this->name} ans his age is {$this->age}";
    }

}

Enter fullscreen mode Exit fullscreen mode

Then we extend the class

class Administration extends Student{

}
$stu=new Administration();
$stu->name="Mark";
$stu->age=40;

$stu->profile();



Enter fullscreen mode Exit fullscreen mode

If we see the output now we can see that the child class can inherit to all properties and methods of parent class they can also assign values to properties of parent class
Alt Text
Here we see that we get the output by inheriting all properties from the parent class. We inherited the class and methods from the parent class.

Another example if we see with magic method construct

class Student{
    public $name;
    public $age;

    public function __construct($name,$age)
    {
        # code...
        $this->name=$name;
        $this->age=$age;
    }
}
Enter fullscreen mode Exit fullscreen mode

Then we extend the class by the a new class so that we can inherit the properties of Student class

class Administration extends Student{
    public $grade;
     public function __construct($name,$age,$grade)
     {
         $this->grade=$grade;
         parent::__construct($name,$age,$grade);

     }
     public function profile()
     {
         # code...
         echo "His name is {$this->name} his age is {$this->age} and he is in grade {$this->grade}";
     }
}


Enter fullscreen mode Exit fullscreen mode

Here we see inside Administration class which is the children class

public function __construct($name,$age,$grade)
     {
         $this->grade=$grade;
         parent::__construct($name,$age,$grade);

     }

Enter fullscreen mode Exit fullscreen mode

Through the parent construct method we can easily inherit the properties assigned from the parent class in the construct method.
Otherwise without parent construct we had to write

public function __construct($name,$age,$grade)
     {
         $this->grade=$grade;
        $this->name = $name;
        $this->age = $age;
        //  parent::__construct($name,$age,$grade);

     }


$stu=new Administration("John",25,2);
$stu->profile();

Enter fullscreen mode Exit fullscreen mode

3.Encapsulation
It consists of 3 types of properties public,private and protected

<?php 
//Encaplusation there are 3 properties public,private,protected 
class Student{
    public $age;
    private $result;
    protected $address;
}

//Public property can be accessed from anywhere both inside and outside of class 
$stu=new Student();
echo $stu->age=25;
//private property id accessed out of the class will give us an error
 echo $stu->result;
//Even if we try to access protected property it gives us an error 
echo $stu->address;
 ?>

Enter fullscreen mode Exit fullscreen mode

Public property can be accessed from anywhere ,But when we try to access the private property it gives us an error
When we try to access private property it gives us an error like below

Alt Text
Even if we try to access protected property it gives us an error
Alt Text

To access private property we need to use setter and getter method
<?php
//Encaplusation there are 3 properties public,private,protected

class Student{
    public $age;
    private $result;
    protected $address;

    public function setResult($result){
        $this->result=$result;
    }
    public function getResult()
    {
        # code...
        echo "His result is {$this->result}";
    }
}
Enter fullscreen mode Exit fullscreen mode

We see in setResult function we set the value of private property result. In getResult we pass a parameter $result which is the private property we can name it anything we want.
//Public property can be accessed from anyhwere both inside and outside of class

$stu=new Student();
echo $stu->age=25;
echo "<br>";
$stu->setResult(20);
$stu->getResult();
Enter fullscreen mode Exit fullscreen mode

//private property id accessed out of the class will give us an error
// echo $stu->result;
// echo "
";

Here we see just by accessing both methods setResult and getResult through $stu we can easily have access to private property $result. In setResult we need pass an argument 20 .
Alt Text

This is what we get as we assigned 20 as an argument in getResult method

class Adminstration extends Student{

    public function __construct($address)
    {
        # code...
        $this->address=$address;
    }
    public function getAddress(){
        echo "His address is {$this->address}";

    }

}
Enter fullscreen mode Exit fullscreen mode

Here we have extended this class and used a constructor to have access to protected property

$admin=new Adminstration("United Kingdom");
$admin->getAddress();
Enter fullscreen mode Exit fullscreen mode

We here create a new object Adminstration and assign a argument United Kingdom to constructor and then by accessing the method gteAddress(); So if we need to have access to the protected property we need to extend the class

Top comments (2)

Collapse
 
behzodjon profile image
behzodjon

Thanks for an amazing and useful post!Please post about interface and abstract classes!!!

Collapse
 
tanzimibthesam profile image
Tanzim Ibthesam

Thanks so much in next part I will explain about parent construct abstract classes method overriding n static.classes then there will be traits n interfaces in part 3 stay tuned.