__debugInfo ක්රමය
__debugInfo ක්රමය අමතන්නේ වස්තුවක් පිළිබඳ තොරතුරු var_dump සහ print_r ශ්රිත මගින් ප්රතිදර්ශනය කරන විට ය. එය ප්රදර්ශනය කිරීමට අවශ්ය ගුණාංග අඩංගු අරාවක් ආපසු ලබා දිය යුතුය. මෙය රහස්ය දත්ත සැඟවීමට හෝ අමතර දෝෂ නිරාකරණ තොරතුරු එක් කිරීමට ඉඩ සලසයි.
වාක්ය වින්යාසය
public function __debugInfo(): array
උදාහරණය
වස්තුව dump කරන විට රහස්ය දත්ත සඟවීම:
<?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
)