DEV Community

Tanzim Ibthesam
Tanzim Ibthesam

Posted on

 

Php OOP Part-2

It is a sequel to my PHP OOP Part-1.Here I am going to talk about parent constructor, static classes, method overriding, abstract classes
1-Parent Constructor

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

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


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

Enter fullscreen mode Exit fullscreen mode

Here we take 2 class and have 2 properties assign the value in constructor and echo them in profile method.
Then we

 class Adminstration extends Student{
     public $grade;

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

         parent::__construct($name,$age);


     }
     public function profile()
     {
     echo "His name is {$this->name} and age is {$this->age} and 
 grade is {$this->grade}";
     }



 }

$admin=new Adminstration("Tanzim",25,4);
$admin-> adminprofile();
Enter fullscreen mode Exit fullscreen mode

Here we see we extend the student class with Administration class in Administration class we assign a new property $grade and then inside construct method we make use of parent::construct to access properties inside constructor method of Student which is parent class here.
2.Static Classes

<?php 



class Student{
    public static $age=15;

    public static function profile(){
        echo "His age is ".self::$age;
    }

}

echo Student::$age;
echo Student::profile();



?>
Enter fullscreen mode Exit fullscreen mode

Static properties and methods are what we can access without instantiating classes like here age is a static property and profile is a static function.
We can just access property just by

echo Student::$age;

 public static function profile(){
        echo "His age is ".self::$age;
    }
echo Student::profile();
Enter fullscreen mode Exit fullscreen mode

To access any property inside a static function we need to use self keyword.

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

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


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

 class Adminstration extends Student{
     public $grade;

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

         parent::__construct($name,$age);


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



 }

$admin=new Adminstration("Tanzim",25,4);
$admin-> adminprofile();

Enter fullscreen mode Exit fullscreen mode

Method Overriding
When we extend the parent class to inherit properties and methods if we instantiate the children class which extends the parent class and both parent class and child class or inherited class have method with same name than the class overrides the method of parent class and replaces it with same method name child class

<?php 

class Student{
    public function profile(){
        echo "Profile from student class";

    }

}

class Adminstration extends Student{
    // public function profile(){
    //     echo "Profile from adminstration class";

    // }


}
$stu=new Student;

// $stu->profile();
$admin=new Adminstration;
$admin->profile();
Enter fullscreen mode Exit fullscreen mode

Here we see class Administration which is the child class which inherits properties and methods of parent class here we see when we instantiate the child class Administration and point out the method profile it gives us methods of parent class
Alt Text
Then if we write a class

<?php 

class Student{
    public function profile(){
        echo "Profile from student class";

    }

}

class Adminstration extends Student{
    public function profile(){
        echo "Profile from adminstration class";

    }


}
$stu=new Student;


$admin=new Adminstration;
$admin->profile();
Enter fullscreen mode Exit fullscreen mode

Here if we see that Adminstration which is child class extending parent class and here both child and parent class have the same method named profile.In this case this is the output we get.
Alt Text
Here we see the method in child class overrides properties in parent class we get the property from child class
Abstract Classes
Abstract classes are classes which can only be used when another class extends it

<?php 

abstract class Student{
    public function profile(){
        echo "Profile";
    }
}


$tech=new Student;

$tech->profile();
?>
Enter fullscreen mode Exit fullscreen mode

Here if we just we instantiate the class we get this error
Alt Text
But if we write

<?php 

abstract class Student{
    public function profile(){
        echo "Profile";
    }
}

class Teacher extends Student{

}
$tech=new Student;

$tech->profile();

?>
Enter fullscreen mode Exit fullscreen mode

Here if we extend the class and instantiate the child class we can get access to the parent class.We will get profile printed from parent class
So its part 2 of php oop hopefully in next part we will discuss some concepts such as Interface,traits etc

Top comments (0)

Advice For Junior Developers

Advice from a career of 15+ years for new and beginner developers just getting started on their journey.