আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Object Oriented Programming Part-8: Abstract Class and Methods

What is Abstract Class in PHP?

 Abstract Class and Methods

Abstract Class in PHP is a special class from which an object can not instantiate or be created. But from Abstract Classes you can create a child class or can inherit. To declare a class an abstract, you have to give an abstract keyword in front of class keyword. Such as “abstract class className {….}”. If there is one or more abstract method in a class, we must declare the class as Abstract class. However, Abstract Class can have abstract methods and non-abstract methods.

What is Abstract Method in PHP?

In PHP, Abstract Method is special Abstract Class Method whose body contains only a Method Signature but doesn’t contain any code, That’s mean, only the Name of the Method and the Parameters are declared. To declare a method abstract, you have to give an abstract keyword in front of the function keyword. Then we have to give parenthesis “()” then semicolon “;”. Such as “abstract function functionName ();”. Abstract Methods must be implemented in the child class with the same parameter. That’s mean, You can not add or remove a parameter of any Method in the child class. But you can add a parameter with the default value. The visibility of the Abstract methods should be kept the same or more open, which means keep them protected or kept public. The abstract method can not be declared private.

Now let us see some examples of abstract class.

Example 1:

<?php
abstract class AbstractClass{
    // Our abstract method only needs to define the required arguments
    abstract protected function getName($name);
    }
 
class childClass extends AbstractClass{
    public function getName($name) {
        return "Hi ".$name." !";
    }
}
 
$class = new childClass;
echo $class->getName("Shahriar"), "\n";
?>

Result:

Hi! Shahriar

Example 2:

Now we will see what could be the problem if implementing the child class without the same parameter.

<?php
abstract class AbstractClass{
    // Our abstract method only needs to define the required arguments
    abstract protected function getName($name);
    }
 
class childClass extends AbstractClass{
    public function getName($name,$prefix) {
        return "Hi ".$name." !";
    }
}
 
$class = new childClass;
echo $class->getName("Shahriar", "Mr. "), "\n";
?>

Result:

Fatal error: Declaration of childClass::getName($name, $prefix) must be compatible with AbstractClass::getName($name) in [...][...]on line 11

Explanation: Notice that there was a Parameter in the parent class at getName Method, Now, giving a extra Parameter without any default value in child class, PHP shows a fatal error.

Example 3:

Now we will see how to implement in the child class without the same parameter.

<?php
abstract class AbstractClass{
    // Our abstract method only needs to define the required arguments
    abstract protected function getName($name);
    }
 
class childClass extends AbstractClass{
    public function getName($name,$prefix="Mr. ") {
        return "Hi ".$prefix.$name." !";
    }
}
 
$class = new childClass;
echo $class->getName("Shahriar"), "\n";
?>

Result:

Hi Mr. Shahriar !

Explanation: Notice that there was a Parameter in the parent class at getName Method. Now showing results without any error in child class, even after providing additional parameter with default value.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *