Advertisement
sebbu

test __debugInfo()

Mar 4th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <?php
  2. var_dump(ReflectionProperty::IS_PUBLIC, ReflectionProperty::IS_PROTECTED, ReflectionProperty::IS_PRIVATE, ReflectionProperty::IS_STATIC);
  3. echo 'normal class'."\r\n";
  4. class MyTestClass1
  5. {
  6.     public $a = 1;
  7.     protected $b = 2;
  8.     private $c = 3;
  9.     public static $z = 1;
  10.     protected static $y = 2;
  11.     private static $x = 3;
  12. }
  13. $test1=new MyTestClass1();
  14. var_dump($test1);
  15. echo 'normal class with naive __debugInfo'."\r\n";
  16. class MyTestClass2
  17. {
  18.     public $a = 1;
  19.     protected $b = 2;
  20.     private $c = 3;
  21.     public static $z = 1;
  22.     protected static $y = 2;
  23.     private static $x = 3;
  24.     public function __debugInfo()
  25.     {
  26.         $rc = new ReflectionClass($this);
  27.         $props = $rc->getProperties(); // BUG ? static members re always returned if public/protected/private
  28.         $ar = array();
  29.         foreach($props as $prop)
  30.         {
  31.             // $ar[ $prop->getName() ] = $this->{$prop->getName()}; // NOTE : works for now...
  32.             $prop->setAccessible(true); $ar[ $prop->getName() ] = $prop->getValue($this);
  33.         }
  34.         return $ar;
  35.     }
  36. }
  37. $test2=new MyTestClass2();
  38. var_dump($test2);
  39. var_dump(get_object_vars($test2));
  40. echo 'normal class with complete __debugInfo'."\r\n";
  41. class MyTestClass3
  42. {
  43.     public $a = 1;
  44.     protected $b = 2;
  45.     private $c = 3;
  46.     public static $z = 1;
  47.     protected static $y = 2;
  48.     private static $x = 3;
  49.     public function __debugInfo()
  50.     {
  51.         $rc = new ReflectionClass($this);
  52.         $props = $rc->getProperties();
  53.         $class = $rc->getName();
  54.         $ar = array();
  55.         foreach($props as $prop)
  56.         {
  57.             if($prop->isStatic()) continue; // BUG ?
  58.             if($prop->getDeclaringClass()->getName()!==get_class($this)) continue; // fix because of workaround further down
  59.             $pri=$prop->isPrivate();
  60.             $pro=$prop->isProtected();
  61.             // NOTE : protected is \0*\0, private is \0class\0
  62.             //$prefix=($pro?"\0*\0":($pri?"\0".get_class($this)."\0":'')); // NOTE : did work, but...
  63.             $prefix=($pro?"\0*\0":($pri?"\0".$prop->getDeclaringClass()->getName()."\0":''));
  64.             //$ar[ $prefix.$prop->getName() ] = $this->{$prop->getName()}; // NOTE : still work here, but... (see down)
  65.             $prop->setAccessible(true); $ar[ $prefix.$prop->getName() ] = $prop->getValue($this);
  66.         }
  67.         // BUG ? : private members of parents classes are missing
  68.         $rc2=$rc;
  69.         while($rc2=$rc2->getParentClass())
  70.         {
  71.             $props2 = $rc2->getProperties();
  72.             $class2 = $rc2->getName();
  73.             foreach($props2 as $prop2)
  74.             {
  75.                 if($prop2->isStatic()) continue; // BUG ?
  76.                 if($prop2->getDeclaringClass()->getName() !== $class2) continue;
  77.                 $pri=$prop2->isPrivate();
  78.                 $pro=$prop2->isProtected();
  79.                 $prefix=($pri?"\0$class2\0":($pro?"\0*\0":''));
  80.                 $prop2->setAccessible(true); $ar[ $prefix. $prop2->getName() ] = $prop2->getValue($this); // NOTE : no other way, direct access through $this doesn't work anymore
  81.             }
  82.         }
  83.         return $ar;
  84.     }
  85. }
  86. $test3=new MyTestClass3();
  87. var_dump($test3);
  88. echo 'first herited class'."\r\n";
  89. class MyTestClass4 extends MyTestClass1
  90. {
  91.     public $d = 4;
  92.     protected $e = 5;
  93.     private $f = 6;
  94.     public static $w = 4;
  95.     protected static $v = 5;
  96.     private static $u = 6;
  97. }
  98. $test4 = new MyTestClass4();
  99. var_dump($test4);
  100. echo 'first herited class with complete debugInfo'."\r\n";
  101. class MyTestClass5 extends MyTestClass3
  102. {
  103.     public $d = 4;
  104.     protected $e = 5;
  105.     private $f = 6;
  106.     public static $w = 4;
  107.     protected static $v = 5;
  108.     private static $u = 6;
  109. }
  110. $test5 = new MyTestClass5();
  111. var_dump($test5);
  112. echo 'second herited class'."\r\n";
  113. class MyTestClass6 extends MyTestClass4
  114. {
  115.     public $g = 7;
  116.     protected $h = 8;
  117.     private $i = 9;
  118.     public static $t = 7;
  119.     protected static $s = 8;
  120.     private static $r = 9;
  121. }
  122. $test6 = new MyTestClass6();
  123. var_dump($test6);
  124. echo 'second herited class with complete debugInfo'."\r\n";
  125. class MyTestClass7 extends MyTestClass5
  126. {
  127.     public $g = 7;
  128.     protected $h = 8;
  129.     private $i = 9;
  130.     public static $t = 7;
  131.     protected static $s = 8;
  132.     private static $r = 9;
  133. }
  134. $test7 = new MyTestClass7();
  135. var_dump($test7);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement