007.phpt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. echo "====>newInstance(25)\n";
  27. try
  28. {
  29. var_dump($ref->newInstance(25));
  30. }
  31. catch (ReflectionException $e)
  32. {
  33. var_dump($e->getMessage());
  34. }
  35. echo "====>newInstance(25, 42)\n";
  36. try
  37. {
  38. var_dump($ref->newInstance(25, 42));
  39. }
  40. catch (ReflectionException $e)
  41. {
  42. var_dump($e->getMessage());
  43. }
  44. echo "\n";
  45. }
  46. function __autoload($class)
  47. {
  48. echo __FUNCTION__ . "($class)\n";
  49. }
  50. test('Class_does_not_exist');
  51. Class NoCtor
  52. {
  53. }
  54. test('NoCtor');
  55. Class WithCtor
  56. {
  57. function __construct()
  58. {
  59. echo __METHOD__ . "()\n";
  60. var_dump(func_get_args());
  61. }
  62. }
  63. test('WithCtor');
  64. Class WithCtorWithArgs
  65. {
  66. function __construct($arg)
  67. {
  68. echo __METHOD__ . "($arg)\n";
  69. var_dump(func_get_args());
  70. }
  71. }
  72. test('WithCtorWithArgs');
  73. ?>
  74. ===DONE===
  75. <?php exit(0); ?>
  76. --EXPECTF--
  77. ====>Class_does_not_exist
  78. __autoload(Class_does_not_exist)
  79. string(41) "Class Class_does_not_exist does not exist"
  80. ====>NoCtor
  81. ====>newInstance()
  82. object(NoCtor)#%d (0) {
  83. }
  84. ====>newInstance(25)
  85. string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments"
  86. ====>newInstance(25, 42)
  87. string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments"
  88. ====>WithCtor
  89. ====>newInstance()
  90. WithCtor::__construct()
  91. array(0) {
  92. }
  93. object(WithCtor)#%d (0) {
  94. }
  95. ====>newInstance(25)
  96. WithCtor::__construct()
  97. array(1) {
  98. [0]=>
  99. int(25)
  100. }
  101. object(WithCtor)#%d (0) {
  102. }
  103. ====>newInstance(25, 42)
  104. WithCtor::__construct()
  105. array(2) {
  106. [0]=>
  107. int(25)
  108. [1]=>
  109. int(42)
  110. }
  111. object(WithCtor)#%d (0) {
  112. }
  113. ====>WithCtorWithArgs
  114. ====>newInstance()
  115. Warning: Missing argument 1 for WithCtorWithArgs::__construct() in %s007.php on line %d
  116. Notice: Undefined variable: arg in %s007.php on line %d
  117. WithCtorWithArgs::__construct()
  118. array(0) {
  119. }
  120. object(WithCtorWithArgs)#%d (0) {
  121. }
  122. ====>newInstance(25)
  123. WithCtorWithArgs::__construct(25)
  124. array(1) {
  125. [0]=>
  126. int(25)
  127. }
  128. object(WithCtorWithArgs)#%d (0) {
  129. }
  130. ====>newInstance(25, 42)
  131. WithCtorWithArgs::__construct(25)
  132. array(2) {
  133. [0]=>
  134. int(25)
  135. [1]=>
  136. int(42)
  137. }
  138. object(WithCtorWithArgs)#%d (0) {
  139. }
  140. ===DONE===