Метад __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
)