OOP Php part-1
Tags: php, oop
Published: 2021-03-03 23:00:17

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;
}
 
?>
```
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;

```
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";

```
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";

```
**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}";
    }
}


```
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();
```

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();
 
?>
```
The output we get is 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5ngvzg8f0cd66ryfjoo.png)
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>";

```
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();
?>
```
Here we see just upon instantiating the class we get what we printed inside the construct class.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo26kfkfoaspifoogoyo.png)
We can easily put our properties in here and assign parameter as variables
```
 public function __construct($name,$age)
    {
        $this->name=$name;
        $this->age=$age;
    }


```
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();

```
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}";
    }
 
}

```
Then we extend the class 
```
class Administration extends Student{
 
}
$stu=new Administration();
$stu->name="Mark";
$stu->age=40;
 
$stu->profile();



```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fudikcqwc1ag4gq1xm4j.png)
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;
    }
}
```
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}";
     }
}


```
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);
 
     }

```
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();

```

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

```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnb822o2a5a68pqvsz73.png)
Even if we try to access protected property it gives us an error
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crqo7dtrwcyahdf331s3.png)


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}";
    }
}
```
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();
```
//private property id accessed out of the class will give us an error
//  echo $stu->result;
//  echo "<br>";
 
 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mziowv0i85msh28yv9rl.png)


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}";
 
    }
 
}
```
Here we have extended this class and used a constructor to have access to protected property 
```
$admin=new Adminstration("United Kingdom");
$admin->getAddress();
```

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