007.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. --TEST--
  2. ReflectionClass::newInstance[Args]
  3. --FILE--
  4. <?php
  5. function test($class)
  6. {
  7. echo "====>$class\n";
  8. try
  9. {
  10. $ref = new ReflectionClass($class);
  11. }
  12. catch (ReflectionException $e)
  13. {
  14. var_dump($e->getMessage());
  15. return; // only here
  16. }
  17. echo "====>newInstance()\n";
  18. try
  19. {
  20. var_dump($ref->newInstance());
  21. }
  22. catch (ReflectionException $e)
  23. {
  24. var_dump($e->getMessage());
  25. }
  26. catch (Throwable $e)
  27. {
  28. echo "Exception: " . $e->getMessage() . "\n";
  29. }
  30. echo "====>newInstance(25)\n";
  31. try
  32. {
  33. var_dump($ref->newInstance(25));
  34. }
  35. catch (ReflectionException $e)
  36. {
  37. var_dump($e->getMessage());
  38. }
  39. echo "====>newInstance(25, 42)\n";
  40. try
  41. {
  42. var_dump($ref->newInstance(25, 42));
  43. }
  44. catch (ReflectionException $e)
  45. {
  46. var_dump($e->getMessage());
  47. }
  48. echo "\n";
  49. }
  50. spl_autoload_register(function ($class) {
  51. echo __FUNCTION__ . "($class)\n";
  52. });
  53. test('Class_does_not_exist');
  54. Class NoCtor
  55. {
  56. }
  57. test('NoCtor');
  58. Class WithCtor
  59. {
  60. function __construct()
  61. {
  62. echo __METHOD__ . "()\n";
  63. var_dump(func_get_args());
  64. }
  65. }
  66. test('WithCtor');
  67. Class WithCtorWithArgs
  68. {
  69. function __construct($arg)
  70. {
  71. echo __METHOD__ . "($arg)\n";
  72. var_dump(func_get_args());
  73. }
  74. }
  75. test('WithCtorWithArgs');
  76. ?>
  77. --EXPECTF--
  78. ====>Class_does_not_exist
  79. {closure}(Class_does_not_exist)
  80. string(43) "Class "Class_does_not_exist" does not exist"
  81. ====>NoCtor
  82. ====>newInstance()
  83. object(NoCtor)#%d (0) {
  84. }
  85. ====>newInstance(25)
  86. string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments"
  87. ====>newInstance(25, 42)
  88. string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments"
  89. ====>WithCtor
  90. ====>newInstance()
  91. WithCtor::__construct()
  92. array(0) {
  93. }
  94. object(WithCtor)#%d (0) {
  95. }
  96. ====>newInstance(25)
  97. WithCtor::__construct()
  98. array(1) {
  99. [0]=>
  100. int(25)
  101. }
  102. object(WithCtor)#%d (0) {
  103. }
  104. ====>newInstance(25, 42)
  105. WithCtor::__construct()
  106. array(2) {
  107. [0]=>
  108. int(25)
  109. [1]=>
  110. int(42)
  111. }
  112. object(WithCtor)#%d (0) {
  113. }
  114. ====>WithCtorWithArgs
  115. ====>newInstance()
  116. Exception: Too few arguments to function WithCtorWithArgs::__construct(), 0 passed and exactly 1 expected
  117. ====>newInstance(25)
  118. WithCtorWithArgs::__construct(25)
  119. array(1) {
  120. [0]=>
  121. int(25)
  122. }
  123. object(WithCtorWithArgs)#%d (0) {
  124. }
  125. ====>newInstance(25, 42)
  126. WithCtorWithArgs::__construct(25)
  127. array(2) {
  128. [0]=>
  129. int(25)
  130. [1]=>
  131. int(42)
  132. }
  133. object(WithCtorWithArgs)#%d (0) {
  134. }