__debugInfo মেথড
__debugInfo মেথডটি var_dump এবং print_r ফাংশন দ্বারা একটি অবজেক্টের তথ্য প্রদর্শন করার সময় কল করা হয়। এটিকে অবশ্যই那些 বৈশিষ্ট্যের একটি অ্যারে রিটার্ন করতে হবে যা প্রদর্শন করতে হবে। এটি গোপন তথ্য লুকানো বা অতিরিক্ত ডিবাগ তথ্য যোগ করতে দেয়।
সিনট্যাক্স
public function __debugInfo(): array
উদাহরণ
একটি অবজেক্ট ডাম্প করার সময় গোপন তথ্য লুকানো:
<?php
class User {
private $password;
public $name;
public function __construct($name, $password) {
$this->name = $name;
$this->password = $password;
}
public function __debugInfo() {
return [
'name' => $this->name,
'password' => '******'
];
}
}
$user = new User('John', 'secret123');
var_dump($user);
?>
কোড 실행ের ফলাফল:
object(User)#1 (2) {
["name"]=> string(4) "John"
["password"]=> string(6) "******"
}
উদাহরণ
অতিরিক্ত ডিবাগ তথ্য যোগ করা:
<?php
class Product {
public $id;
public $price;
public function __construct($id, $price) {
$this->id = $id;
$this->price = $price;
}
public function __debugInfo() {
return [
'id' => $this->id,
'price' => $this->price,
'price_with_tax' => $this->price * 1.2
];
}
}
$product = new Product(101, 100);
var_dump($product);
?>
কোড 실행ের ফলাফল:
object(Product)#1 (3) {
["id"]=> int(101)
["price"]=> int(100)
["price_with_tax"]=> float(120)
}
উদাহরণ
প্রদর্শনের জন্য বৈশিষ্ট্য ফিল্টার করা:
<?php
class Config {
private $dbHost = 'localhost';
private $dbUser = 'admin';
private $cacheEnabled = true;
public function __debugInfo() {
return [
'cacheEnabled' => $this->cacheEnabled
];
}
}
$config = new Config();
print_r($config);
?>
কোড 실행ের ফলাফল:
Config Object (
[cacheEnabled] => 1
)