স্ট্যাটিক মেথড এবং PHP-এর OOP-তে $this

স্ট্যাটিক মেথডের ভিতরে $this অপ্রাপ্য। এটি ঘটে কারণ স্ট্যাটিক মেথডগুলি ক্লাসের নামে সরাসরি অ্যাক্সেস করে, অবজেক্টের কনটেক্সট ছাড়াই কল করা যেতে পারে।

সাধারণ মেথডে স্ট্যাটিক এবং সাধারণ উভয় প্রোপার্টি ও মেথড উপলব্ধ।

আসুন উদাহরণের মাধ্যমে দেখি। একটি সাধারণ মেথডের উদাহরণ:

Test.php<?php
  class Test
  {
    public static $staticProperty = 'static';
    public $usualProperty = 'usual';
    
    public function method()
    {
      echo self::$staticProperty; // আউটপুট হবে 'static'
      echo $this->usualProperty;  // আউটপুট হবে 'usual'
    }
  }
  
  $test = new Test;
  $test->method();
?>

স্ট্যাটিক মেথডের উদাহরণ:

Test.php<?php
  class Test
  {
    public static $staticProperty = 'static';
    public $usualProperty = 'usual';
    
    public static function method()
    {
      echo self::$staticProperty; // আউটপুট হবে 'static'
      echo $this->usualProperty;  // একটি ত্রুটি দেবে
    }
  }
  
  $test = new Test;
  $test::method();
?>