fscanf_variation54.phpt 1.5 KB

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