Breaking

Post Top Ad

Your Ad Spot

Thursday, 27 September 2018

PHP তে Abstract Class & Method

PHP তে Abstract Class কি?

PHP তে Abstract Class হচ্ছে এক ধরনের বিশেষ class যার থেকে কোনো Object instantiate বা তৈরী করা যাবেনা। কিন্তু Abstract Class গুলো থেকে child class তৈরী বা inherit করা যাবে। কোনো class কে abstract ঘোষণা করতে হলে class keyword এর সামনে abstract keyword টি দিতে হয়। “যেমন- abstract class className{....}” . 
কোন class এর মধ্যে যদি এক বা একাধিক abstract Method থাকে, সেটিকে অবশ্যই Abstract class হিসেবে Declare করতে হবে । তবে Abstract Class এ abstract এবং Non-Abstract দুই ধরনের মেথডই থাকতে পারে।

Abstract Class এর বৈশিষ্ট্য ঃ

   -> Abstract Class হোলও সেই ধরনের class যাদের directly object তৈরি করা যায় না ।
   -> Abstract Class এর Object instantiate বা তৈরী করা যায় না ।মানে Abstract Class কে directly initralize করা যায় না ।
   -> Abstract Class এর object তৈরি করা যায় না ।
   -> Abstract CLass use হয় শুধুমাত্র inheritance purpose এ ।
   -> Abstract Class থেকে child class তৈরী বা inherit করা যাবে । 
   -> Abstract Class কে অবশ্যই inherit করতে হবে ।
   -> Abstract Method গুলোকে সম সংখ্যক Parameter সহ child class এ অবশ্যই implement করতে হবে অর্থাৎ, আপনি চাইলেই child class এ কোন Method এর একটি Parameter যোগ বা বাদ দিতে পারবেন না ।তবে default value সহ Parameter যোগ করতে পারবেন।
   -> কোনো class কে abstract ঘোষণা করতে হলে class keyword এর সামনে abstract keyword টি দিতে হয়
   -> কোন class এর মধ্যে যদি এক বা একাধিক abstract Method থাকে, সেটিকে অবশ্যই Abstract class হিসেবে Declare করতে হবে
   ->  Abstract Class এ abstract এবং Non-Abstract দুই ধরনের মেথডই থাকতে পারে।
   -> Abstract Class এর method এর body তে কোন code define করা থাকে না অর্থাৎ শুধুমাত্র Method এর নাম এবং Parameter সমূহ Declare করা থাকে।
   -> কোনো Method কে abstract ঘোষণা করতে হলে function keyword এর সামনে abstract keyword টি দিতে হয় abstract function functionName();
   -> Abstract Method গুলোর Visibility একই অথবা বেশি Open রাখতে হবে অর্থাৎ protected থাকলে protected অথবা public রাখতে হবে।
   -> Abstract method কে private ঘোষণা করা যায়না।
   -> Abstract Class is not possible to implement multiple inheritance.
   -> Latest version of PHP 5 has introduces abstract classes and methods.

Abstract Method এর বৈশিষ্ট্য ঃ

  -> Abstract method এর body থাকে না ।
  -> Abstract method কে private ঘোষণা করা যায়না।
  -> Abstract method হোলও simply a function definition যে method কে অবশ্যই child class এ implement করতে হবে ।
  -> Child Class এ অবশ্যই Abstract Class এর method কে define করতে হবে । 
      Abstract Method গুলোকে সম সংখ্যক Parameter সহ child class এ অবশ্যই implement করতে হবে অর্থাৎ, আপনি চাইলেই child class এ কোন Method এর একটি Parameter যোগ বা বাদ দিতে পারবেন না ।তবে default value সহ Parameter যোগ করতে পারবেন।

Example Code:
abstract class A {
    public function test1() {
        echo 'Hello World';
    }
    abstract protected function f1();
    abstract public function f2();
    protected function test2(){
        echo 'Hello World test';
    }
}

class B extends A {
    public $a = 'India';
    public function f1() {
        echo "F1 Method Call";
    }
    public function f2() {
        echo "F2 Method Call";
    }
}

$b = new B();
echo $b->test1() . "<br/>";
echo $b->a . "<br/>";
echo $b->test2() . "<br/>";
echo $b->f1() . "<br/>";
echo $b->f2() . "<br/>";
Output:
Hello World
India
Hello World test
F1 Method Call
F2 Method Call

PHP তে Abstract Method কি?

PHP তে Abstract Method একধরণের বিশেষ Abstract Class Method যার বডিতে কোনো code define করা থাকেনা শুধু Method Signature থাকে , অর্থাৎ শুধুমাত্র Method এর নাম এবং Parameter সমূহ Declare করা থাকে। কোনো Method কে abstract ঘোষণা করতে হলে function keyword এর সামনে abstract keyword টি দিতে হয়। তারপর perentheses “( )” তারপর semicolon ” ; ” দিতে হয়। “যেমন- abstract function functionName();“. Abstract Method গুলোকে সম সংখ্যক Parameter সহ child class এ অবশ্যই implement করতে হবে। অর্থাৎ, আপনি চাইলেই child class এ কোন Method এর একটি Parameter যোগ বা বাদ দিতে পারবেন না । তবে default value সহ Parameter যোগ করতে পারবেন। Abstract Method গুলোর Visibility একই অথবা বেশি Open রাখতে হবে অর্থাৎ protected থাকলে protected অথবা public রাখতে হবে। abstract method কে private ঘোষণা করা যায়না।
এবার চলুন abastract class নিয়ে কয়েকটা উদাহরণ দেখা যাক।

উদাহরণ ১:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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

উদাহরণ ২:

Abstract Method গুলোকে সম সংখ্যক Parameter সহ child class এ অবশ্যই implement করতে হবে অর্থাৎ, আপনি চাইলেই child class এ কোন Method এর একটি Parameter যোগ বা বাদ দিতে পারবেন না ।তবে default value সহ Parameter যোগ করতে পারবেন।
এবার আমরা দেখবো সম সংখ্যক Parameter ছাড়া child class এ implement করলে কি সমস্যা হতে পারে।
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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
ব্যাখ্যা: লক্ষ্য করুন parent class এ getName Method এ Parameter ছিল একটি , এখন child class এ কোনো default value ছাড়া অতিরিক্ত Parameter দেয়াই PHP একটি fatal error দেখাচ্ছে।

উদাহরণ ৩:

এবার আমরা দেখবো সম সংখ্যক Parameter ছাড়া child class এ implement করার পদ্ধতি ।
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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 !
ব্যাখ্যা: লক্ষ্য করুন parent class এ getName Method এ Parameter ছিল একটি , এখন child class এ default value সহ অতিরিক্ত Parameter দেওয়ার পরও PHP কোনো error ছাড়াই ফলাফল দেখাচ্ছে।

আবারও ......................................................

Characteristics of Abstract Classes

Make a note of these characteristics to lock down your understanding of abstract classes:
  1. Single inheritance. Child classes can extend only one class at a time.
  2. Abstract classes cannot be instantiated — no new Animal();
  3. Abstract classes can define class variables of type const only.
  4. Abstract class A can be extended by another abstract class B. Abstract class B can implement none or any of the abstract functions in A.
  5. In the previous case, a child class C which extends abstract class B must implement all abstract functions in B as well as the abstract functions in A which have not already been implemented in B.
  6. The signature of the concrete functions and abstract functions must be the same. However, if an abstract function is defined as abstract function speak($greeting); then it is okay to implement it as function speak($greeting, $shout = FALSE) but not function speak($greeting, $shout).
  7. The visibility of functions in the child classes must be the same or less restrictive than the parent class. Thus, a protected abstract function can be implemented as either protected or public but not private.
  8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, as of PHP 5.3 this is allowed.
1. Can not instantiate abstract class: Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract.
Example below :
abstract class AbstractClass
{

    abstract protected function getValue();
    abstract protected function prefixValue($prefix);


    public function printOut() {
        echo "Hello how are you?";
    }
}

$obj=new AbstractClass();
$obj->printOut();
//Fatal error: Cannot instantiate abstract class AbstractClass
2. Any class that contains at least one abstract method must also be abstract: Abstract class can have abstract and non-abstract methods, but it must contain at least one abstract method. If a class has at least one abstract method, then the class must be declared abstract.
Note: Traits support the use of abstract methods in order to impose requirements upon the exhibiting class.
Example below :
class Non_Abstract_Class
{
   abstract protected function getValue();

    public function printOut() {
        echo "Hello how are you?";
    }
}

$obj=new Non_Abstract_Class();
$obj->printOut();
//Fatal error: Class Non_Abstract_Class contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Non_Abstract_Class::getValue)
3. An abstract method can not contain body: Methods defined as abstract simply declare the method's signature - they cannot define the implementation. But a non-abstract method can define the implementation.
abstract class AbstractClass
{
   abstract protected function getValue(){
   return "Hello how are you?";
   }

    public function printOut() {
        echo $this->getValue() . "\n";
    }
}

class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}

$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
//Fatal error: Abstract function AbstractClass::getValue() cannot contain body
4. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child :If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();

    // Common method
    public function printOut() {
        print $this->getValue() . "<br/>";
    }
}

class ConcreteClass1 extends AbstractClass
{
    public function printOut() {
        echo "dhairya";
    }

}
$class1 = new ConcreteClass1;
$class1->printOut();
//Fatal error: Class ConcreteClass1 contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (AbstractClass::getValue)
5. Same (or a less restricted) visibility:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
Note that abstract method should not be private.
abstract class AbstractClass
{

    abstract public function getValue();
    abstract protected function prefixValue($prefix);

        public function printOut() {
        print $this->getValue();
    }
}

class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    public function prefixValue($prefix) {
        return "{$prefix}ConcreteClass1";
    }
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."<br/>";
//Fatal error: Access level to ConcreteClass1::getValue() must be public (as in class AbstractClass)
6. Signatures of the abstract methods must match:When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child;the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature.
abstract class AbstractClass
{

    abstract protected function prefixName($name);

}

class ConcreteClass extends AbstractClass
{


    public function prefixName($name, $separator = ".") {
        if ($name == "Pacman") {
            $prefix = "Mr";
        } elseif ($name == "Pacwoman") {
            $prefix = "Mrs";
        } else {
            $prefix = "";
        }
        return "{$prefix}{$separator} {$name}";
    }
}

$class = new ConcreteClass;
echo $class->prefixName("Pacman"), "<br/>";
echo $class->prefixName("Pacwoman"), "<br/>";
//output: Mr. Pacman
//        Mrs. Pacwoman
7. Abstract class doesn't support multiple inheritance:Abstract class can extends another abstract class,Abstract class can provide the implementation of interface.But it doesn't support multiple inheritance.
interface MyInterface{
    public function foo();
    public function bar();
}

abstract class MyAbstract1{
    abstract public function baz();
}


abstract class MyAbstract2 extends MyAbstract1 implements MyInterface{
    public function foo(){ echo "foo"; }
    public function bar(){ echo "bar"; }
    public function baz(){ echo "baz"; }
}

class MyClass extends MyAbstract2{
}

$obj=new MyClass;
$obj->foo();
$obj->bar();
$obj->baz();
//output: foobarbaz
Note: Please note order or positioning of the classes in your code can affect the interpreter and can cause a Fatal error. So, when using multiple levels of abstraction, be careful of the positioning of the classes within the source code.
below example will cause Fatal error: Class 'horse' not found
class cart extends horse {
    public function get_breed() { return "Wood"; }
}

abstract class horse extends animal {
    public function get_breed() { return "Jersey"; }
}

abstract class animal {
    public abstract function get_breed();
}

$cart = new cart();
print($cart->get_breed());

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages