কম্পিউটার

পিএইচপি অবজেক্ট ইন্টারফেস


পরিচয়

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

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

সিনট্যাক্স

<?php
interface testinterface{
   public function testmethod();
}
class testclass implements testinterface{
   public function testmethod(){
      echo "implements interfce 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)

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

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

নিম্নলিখিত উদাহরণে, অভিভাবক শ্রেণীর দুটি বিমূর্ত পদ্ধতি রয়েছে, যার মধ্যে শুধুমাত্র একটি শিশু শ্রেণিতে পুনরায় সংজ্ঞায়িত করা হয়েছে। এর ফলে নিম্নরূপ ত্রুটি দেখা দেয় -

উদাহরণ

<?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 এক্সটেন্ডস ক্লজে একের বেশি ক্লাসের অনুমতি দেয় না। যাইহোক, চাইল্ড ক্লাসকে এক বা একাধিক ইন্টারফেস প্রয়োগ করতে দিয়ে একাধিক উত্তরাধিকার অর্জন করা যেতে পারে

নিম্নলিখিত উদাহরণে, মাইক্লাস টেস্টক্লাস প্রসারিত করে এবং একাধিক উত্তরাধিকার অর্জনের জন্য টেস্টিং ইন্টারফেস প্রয়োগ করে

উদাহরণ

<?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() ফাংশন