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

PHP অবজেক্ট তুলনা


ভূমিকা

PHP-তে একটি তুলনা অপারেটর == রয়েছে, যার মাধ্যমে দুটি অবজেক্ট ভেরিয়েবলের সাধারণ তুলনা করা যায়। এটি true রিটার্ন করে যদি উভয়ই একই ক্লাসের হয়ে থাকে এবং সংশ্লিষ্ট প্রপার্টিগুলোর মান একই থাকে।

PHP-এর === অপারেটর দুটি অবজেক্ট ভেরিয়েবল তুলনা করে এবং true রিটার্ন করে শুধুমাত্র যদি তারা একই ক্লাসের একই ইনস্ট্যান্সকে বোঝায়।

এই অপারেটরগুলো দিয়ে অবজেক্ট তুলনার জন্য আমরা নিচের দুটি ক্লাস ব্যবহার করব:

উদাহরণ

<?php
class test1{
    private $x;
    private $y;
    function __construct($arg1, $arg2){
        $this->x=$arg1;
        $this->y=$arg2;
    }
}
class test2{
    private $x;
    private $y;
    function __construct($arg1, $arg2){
        $this->x=$arg1;
        $this->y=$arg2;
    }
}
?>

একই ক্লাসের দুটি অবজেক্ট

উদাহরণ

$a=new test1(10,20);
$b=new test1(10,20);
echo "two objects of same class\n";
echo "using == operator : ";
var_dump($a==$b);
echo "using === operator : ";
var_dump($a===$b);

আউটপুট

two objects of same class
using == operator : bool(true)
using === operator : bool(false)

একই অবজেক্টের দুটি রেফারেন্স

উদাহরণ

$a=new test1(10,20);
$c=$a;
echo "two references of same object\n";
echo "using == operator : ";
var_dump($a==$c);
echo "using === operator : ";
var_dump($a===$c);

আউটপুট

two references of same object
using == operator : bool(true)
using === operator : bool(true)

ভিন্ন ক্লাসের দুটি অবজেক্ট

উদাহরণ

$a=new test1(10,20);
$d=new test2(10,20);
echo "two objects of different classes\n";
echo "using == operator : ";
var_dump($a==$d);
echo "using === operator : ";
var_dump($a===$d);

আউটপুট

আউটপুট নিচের ফলাফল দেখায়:

two objects of different classes
using == operator : bool(false)
using === operator : bool(false)

  1. PHP-এ !== এবং ==! অপারেটরের পার্থক্য

  2. PHP-তে র‍্যান্ডম স্ট্রিং জেনারেট করা

  3. PHP-তে ডাবল নট (!!) অপারেটর

  4. PHP-তে দুটি ডেট তুলনা করা