কম্পিউটার টিউটোরিয়াল

PHP অবজেক্ট ইন্টারফেস


ভূমিকা

ইন্টারফেস অবজেক্ট-অরিয়েন্টেড প্রোগ্রামিং-এর একটি গুরুত্বপূর্ণ বৈশিষ্ট্য যার মাধ্যমে একটি ক্লাস কী মেথড ইমপ্লিমেন্ট করবে তা নির্দিষ্ট করা যায়, কিন্তু কীভাবে ইমপ্লিমেন্ট করবে তা ডিফাইন করতে হয় না।

PHP interface কিওয়ার্ড দিয়ে ইন্টারফেস সাপোর্ট করে। ইন্টারফেস ক্লাসের অনুরূপ কিন্তু মেথডের বডি থাকে না। ইন্টারফেসের মেথডগুলো অবশ্যই পাবলিক হতে হবে। যে ক্লাসটি এই মেথডগুলো ইমপ্লিমেন্ট করবে সেটি implements কিওয়ার্ড দিয়ে ডিফাইন করতে হবে (extends নয়) এবং প্যারেন্ট ইন্টারফেসের সকল মেথডের ইমপ্লিমেন্টেশন দিতে হবে।

সিনট্যাক্স

<?php
interface testinterface{
    public function testmethod();
}
class testclass implements testinterface{
    public function testmethod(){
        echo "implements interface method";
    }
}
?>

ইন্টারফেসের সকল মেথড ইমপ্লিমেন্টিং ক্লাসে ডিফাইন করতে হবে, অন্যথায় PHP পার্সার এক্সেপশন থ্রো করে।

উদাহরণ

<?php
interface testinterface{
    public function test1();
    public function test2();
}
class testclass implements testinterface{
    public function test1(){
        echo "implements interface method";
    }
}
$obj=new testclass()
?>

আউটপুট

এরর নিচের মতো:

PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)

এক্সটেন্ডেবল ইন্টারফেস

সাধারণ ক্লাসের মতই, একটি ইন্টারফেসও extends কিওয়ার্ড ব্যবহার করে ইনহেরিট করা যায়।

নিচের উদাহরণে, প্যারেন্ট ক্লাসে দুটি অ্যাবস্ট্রাক্ট মেথড আছে, 그중 শুধু একটি চাইল্ড ক্লাসে রিডিফাইন করা হয়েছে। এর ফলে এরর হয়:

উদাহরণ

<?php
interface testinterface{
    public function test1();
}
interface myinterface extends testinterface{
    public function test2();
}
class testclass implements myinterface{
    public function test1(){
        echo "implements test1 method";
    }
    public function test2(){
        echo "implements test2 method";
    }
}
?>

ইন্টারফেস ব্যবহার করে মাল্টিপল ইনহেরিটেন্স

PHP একাধিক ক্লাস এক্সটেন্ডস ক্লজে অনুমতি দেয় না। কিন্তু, চাইল্ড ক্লাস এক বা একাধিক ইন্টারফেস ইমপ্লিমেন্ট করে মাল্টিপল ইনহেরিটেন্স অর্জন করতে পারে।

নিচের উদাহরণে, myclass testclass এক্সটেন্ড করে এবং testinterface ইমপ্লিমেন্ট করে মাল্টিপল ইনহেরিটেন্স অর্জন করেছে:

উদাহরণ

<?php
interface testinterface{
    public function test1();
}
class testclass{
    public function test2(){
        echo "this is test2 function in parent class\n";
    }
}
class myclass extends testclass implements testinterface{
    public function test1(){
        echo "implements test1 method\n";
    }
}
$obj=new myclass();
$obj->test1();
$obj->test2();
?>

আউটপুট

এটি নিচের আউটপুট তৈরি করবে:

implements test1 method
this is test2 function in parent class

ইন্টারফেস উদাহরণ

উদাহরণ

<?php
interface shape{
    public function area();
}
class circle implements shape{
    private $rad;
    public function __construct(){
        $this->rad=5;
    }
    public function area(){
        echo "area of circle=" . M_PI*pow($this->rad,2) ."\n";
    }
}
class rectangle implements shape{
    private $width;
    private $height;
    public function __construct(){
        $this->width=20;
        $this->height=10;
    }
    public function area(){
        echo "area of rectangle=" . $this->width*$this->height ."\n";
    }
}
$c=new circle();
$c->area();
$r=new rectangle();
$r->area();
?>

আউটপুট

উপরোক্ত স্ক্রিপ্ট নিচের ফলাফল তৈরি করে:

area of circle=78.539816339745
area of rectangle=200

  1. PHP-এ method_exists() ফাংশন

  2. PHP-এ is_a() ফাংশন

  3. PHP-এ get_class() ফাংশন

  4. PHP-এ get_object_vars() ফাংশন