ReflectionClass_newInstanceArgs_001.phpt 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. --TEST--
  2. ReflectionClass::newInstanceArgs
  3. --CREDITS--
  4. Robin Fernandes <robinf@php.net>
  5. Steve Seear <stevseea@php.net>
  6. --FILE--
  7. <?php
  8. class B {
  9. public function __construct($a, $b) {
  10. echo "In constructor of class B with args $a, $b\n";
  11. }
  12. }
  13. class C {
  14. protected function __construct() {
  15. echo "In constructor of class C\n";
  16. }
  17. }
  18. class D {
  19. private function __construct() {
  20. echo "In constructor of class D\n";
  21. }
  22. }
  23. class E {
  24. }
  25. $rcB = new ReflectionClass('B');
  26. $rcC = new ReflectionClass('C');
  27. $rcD = new ReflectionClass('D');
  28. $rcE = new ReflectionClass('E');
  29. try {
  30. $rcB->newInstanceArgs();
  31. } catch (Throwable $e) {
  32. echo "Exception: " . $e->getMessage() . "\n";
  33. }
  34. var_dump($rcB->newInstanceArgs(array('x', 123)));
  35. try {
  36. $rcC->newInstanceArgs();
  37. echo "you should not see this\n";
  38. } catch (Exception $e) {
  39. echo $e->getMessage() . "\n";
  40. }
  41. try {
  42. $rcD->newInstanceArgs();
  43. echo "you should not see this\n";
  44. } catch (Exception $e) {
  45. echo $e->getMessage() . "\n";
  46. }
  47. $e1 = $rcE->newInstanceArgs();
  48. var_dump($e1);
  49. try {
  50. $e2 = $rcE->newInstanceArgs(array('x'));
  51. echo "you should not see this\n";
  52. } catch (Exception $e) {
  53. echo $e->getMessage() . "\n";
  54. }
  55. ?>
  56. --EXPECTF--
  57. Exception: Too few arguments to function B::__construct(), 0 passed and exactly 2 expected
  58. In constructor of class B with args x, 123
  59. object(B)#%d (0) {
  60. }
  61. Access to non-public constructor of class C
  62. Access to non-public constructor of class D
  63. object(E)#%d (0) {
  64. }
  65. Class E does not have a constructor, so you cannot pass any constructor arguments