__debugInfo usuly

__debugInfo usuly obýekt baradaky maglumatlar var_dump we print_r funksiýalary tarapyndan çap edilende çagyrylýar. Ol görkeziljek aýratynlyklary ýatda saklamaly bolan sanawy yzyna gaýtarmaly. Bu gizlin maglumatlary gizlemäge ýa-da goşmaça düzediş maglumatlaryny goşmaga mümkinçilik berýär.

Sintaksis

phppublic function __debugInfo(): array

Mysal

Obýekt dämp edilende gizlin maglumatlary gizlemek:

php<?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);
?>

Kodyň ýerine ýetiriliş netijesi:

phpobject(User)#1 (2) {
  ["name"]=> string(4) "John"
  ["password"]=> string(6) "******"
}

Mysal

Goşmaça düzediş maglumatlaryny goşmak:

php<?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);
?>

Kodyň ýerine ýetiriliş netijesi:

phpobject(Product)#1 (3) {
  ["id"]=> int(101)
  ["price"]=> int(100)
  ["price_with_tax"]=> float(120)
}

Mysal

Görkeziş üçin aýratynlyklary filtrlämek:

php<?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);
?>

Kodyň ýerine ýetiriliş netijesi:

phpConfig Object (
  [cacheEnabled] => 1
)

Şeýle-de garaň

  • funksiýa var_dump,
    üýtgeýjilik barada maglumat çap edýär
  • funksiýa print_r,
    üýtgeýjilik barada okalýan maglumat çap edýär