fscanf_variation54.phpt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. --TEST--
  2. Test fscanf() function: usage variations - objects
  3. --FILE--
  4. <?php
  5. /*
  6. Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] );
  7. Description: Parses input from a file according to a format
  8. */
  9. /* Test fscanf() to scan a file to read objects */
  10. $file_path = dirname(__FILE__);
  11. echo "*** Test fscanf(): to read objects from a file ***\n";
  12. // declare a class
  13. class foo
  14. {
  15. public $var1 = 1;
  16. public $var2 = 2;
  17. function __toString() {
  18. return "Object";
  19. }
  20. }
  21. // create an object
  22. $obj = new foo(); //creating new object
  23. // create a file
  24. $filename = "$file_path/fscanf_variation54.tmp";
  25. $file_handle = fopen($filename, "w");
  26. if($file_handle == false)
  27. exit("Error:failed to open file $filename");
  28. //writing object to the file
  29. fwrite($file_handle, $obj);
  30. //closing the file
  31. fclose($file_handle);
  32. // various formats
  33. $formats = array( "%d", "%f", "%e", "%u", " %s", "%x", "%o");
  34. $counter = 1;
  35. // opening file for read
  36. $file_handle = fopen($filename, "r");
  37. if($file_handle == false) {
  38. exit("Error:failed to open file $filename");
  39. }
  40. echo "\n-- iteration $counter --\n";
  41. foreach($formats as $format) {
  42. var_dump( fscanf($file_handle,$format) );
  43. rewind($file_handle);
  44. }
  45. fclose($file_handle);
  46. echo "\n*** Done ***";
  47. ?>
  48. --CLEAN--
  49. <?php
  50. $file_path = dirname(__FILE__);
  51. $filename = "$file_path/fscanf_variation54.tmp";
  52. unlink($filename);
  53. ?>
  54. --EXPECT--
  55. *** Test fscanf(): to read objects from a file ***
  56. -- iteration 1 --
  57. array(1) {
  58. [0]=>
  59. NULL
  60. }
  61. array(1) {
  62. [0]=>
  63. NULL
  64. }
  65. array(1) {
  66. [0]=>
  67. NULL
  68. }
  69. array(1) {
  70. [0]=>
  71. NULL
  72. }
  73. array(1) {
  74. [0]=>
  75. string(6) "Object"
  76. }
  77. array(1) {
  78. [0]=>
  79. NULL
  80. }
  81. array(1) {
  82. [0]=>
  83. NULL
  84. }
  85. *** Done ***