mysqli_fetch_object.phpt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. --TEST--
  2. mysqli_fetch_object()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. ?>
  9. --FILE--
  10. <?php
  11. include_once("connect.inc");
  12. set_error_handler('handle_catchable_fatal');
  13. require('table.inc');
  14. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5")) {
  15. printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  16. }
  17. $obj = mysqli_fetch_object($res);
  18. if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
  19. printf("[004] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  20. var_dump($obj);
  21. }
  22. class mysqli_fetch_object_test {
  23. public $a = null;
  24. public $b = null;
  25. public function toString() {
  26. var_dump($this);
  27. }
  28. }
  29. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_test');
  30. if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
  31. printf("[005] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  32. var_dump($obj);
  33. }
  34. class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
  35. public function __construct($a, $b) {
  36. $this->a = $a;
  37. $this->b = $b;
  38. }
  39. }
  40. try {
  41. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array());
  42. if (($obj->ID !== "3") || ($obj->label !== "c") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
  43. printf("[006] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  44. var_dump($obj);
  45. }
  46. } catch (Throwable $e) {
  47. echo "Exception: " . $e->getMessage() . "\n";
  48. }
  49. try {
  50. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a'));
  51. if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
  52. printf("[007] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  53. var_dump($obj);
  54. }
  55. } catch (Throwable $e) {
  56. echo "Exception: " . $e->getMessage() . "\n";
  57. }
  58. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b'));
  59. if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
  60. printf("[008] Object seems wrong. [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  61. var_dump($obj);
  62. }
  63. var_dump(mysqli_fetch_object($res, 'mysqli_fetch_object_construct', array('a', 'b', 'c')));
  64. var_dump(mysqli_fetch_object($res));
  65. mysqli_free_result($res);
  66. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
  67. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  68. }
  69. mysqli_free_result($res);
  70. try {
  71. mysqli_fetch_object($res);
  72. } catch (Error $exception) {
  73. echo $exception->getMessage() . "\n";
  74. }
  75. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
  76. printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  77. /*
  78. TODO
  79. I'm using the procedural interface, this should not throw an exception.
  80. Also, I did not ask to get exceptions using the mysqli_options()
  81. */
  82. try {
  83. if (false !== ($obj = @mysqli_fetch_object($res, 'mysqli_fetch_object_construct', 'a')))
  84. printf("[011] Should have failed\n");
  85. } catch (Error $e) {
  86. handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
  87. }
  88. mysqli_free_result($res);
  89. if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5"))
  90. printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  91. class mysqli_fetch_object_private_constructor extends mysqli_fetch_object_test {
  92. private function __construct($a, $b) {
  93. $this->a = $a;
  94. $this->b = $b;
  95. }
  96. }
  97. /*
  98. TODO
  99. I think we should bail out here. The following line will give a Fatal error: Call to private ... from invalid context
  100. var_dump($obj = new mysqli_fetch_object_private_constructor(1, 2));
  101. This does not fail.
  102. */
  103. $obj = mysqli_fetch_object($res, 'mysqli_fetch_object_private_constructor', array('a', 'b'));
  104. mysqli_free_result($res);
  105. try {
  106. var_dump(mysqli_fetch_object($res, 'this_class_does_not_exist'));
  107. } catch (TypeError $e) {
  108. echo $e->getMessage(), "\n";
  109. }
  110. mysqli_close($link);
  111. print "done!";
  112. ?>
  113. --CLEAN--
  114. <?php
  115. require_once("clean_table.inc");
  116. ?>
  117. --EXPECTF--
  118. Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 0 passed and exactly 2 expected
  119. Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
  120. NULL
  121. NULL
  122. mysqli_result object is already closed
  123. [0] mysqli_fetch_object(): Argument #3 ($constructor_args) must be of type array, string given in %s on line %d
  124. mysqli_fetch_object(): Argument #2 ($class) must be a valid class name, this_class_does_not_exist given
  125. done!