Home | History | Annotate | Download | only in source
      1 Use in PHP    {#flatbuffers_guide_use_php}
      2 ==========
      3 
      4 ## Before you get started
      5 
      6 Before diving into the FlatBuffers usage in PHP, it should be noted that
      7 the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
      8 general FlatBuffers usage in all of the supported languages
      9 (including PHP). This page is specifically designed to cover the nuances of
     10 FlatBuffers usage in PHP.
     11 
     12 You should also have read the [Building](@ref flatbuffers_guide_building)
     13 documentation to build `flatc` and should be familiar with
     14 [Using the schema compiler](@ref flatbuffers_guide_using_schema_compiler) and
     15 [Writing a schema](@ref flatbuffers_guide_writing_schema).
     16 
     17 ## FlatBuffers PHP library code location
     18 
     19 The code for FlatBuffers PHP library can be found at `flatbuffers/php`. You
     20 can browse the library code on the [FlatBuffers
     21 GitHub page](https://github.com/google/flatbuffers/tree/master/php).
     22 
     23 ## Testing the FlatBuffers JavaScript library
     24 
     25 The code to test the PHP library can be found at `flatbuffers/tests`.
     26 The test code itself is located in [phpTest.php](https://github.com/google/
     27 flatbuffers/blob/master/tests/phpTest.php).
     28 
     29 You can run the test with `php phpTest.php` from the command line.
     30 
     31 *Note: The PHP test file requires
     32 [PHP](http://php.net/manual/en/install.php) to be installed.*
     33 
     34 ## Using theFlatBuffers PHP library
     35 
     36 *Note: See [Tutorial](@ref flatbuffers_guide_tutorial) for a more in-depth
     37 example of how to use FlatBuffers in PHP.*
     38 
     39 FlatBuffers supports both reading and writing FlatBuffers in PHP.
     40 
     41 To use FlatBuffers in your own code, first generate PHP classes from your schema
     42 with the `--php` option to `flatc`. Then you can include both FlatBuffers and
     43 the generated code to read or write a FlatBuffer.
     44 
     45 For example, here is how you would read a FlatBuffer binary file in PHP:
     46 First, include the library and generated code (using the PSR `autoload`
     47 function). Then you can read a FlatBuffer binary file, which you
     48 pass the contents of to the `GetRootAsMonster` function:
     49 
     50 ~~~{.php}
     51   // It is recommended that your use PSR autoload when using FlatBuffers in PHP.
     52   // Here is an example:
     53   function __autoload($class_name) {
     54     // The last segment of the class name matches the file name.
     55     $class = substr($class_name, strrpos($class_name, "\\") + 1);
     56     $root_dir = join(DIRECTORY_SEPARATOR, array(dirname(dirname(__FILE__)))); // `flatbuffers` root.
     57 
     58     // Contains the `*.php` files for the FlatBuffers library and the `flatc` generated files.
     59     $paths = array(join(DIRECTORY_SEPARATOR, array($root_dir, "php")),
     60                    join(DIRECTORY_SEPARATOR, array($root_dir, "tests", "MyGame", "Example")));
     61     foreach ($paths as $path) {
     62       $file = join(DIRECTORY_SEPARATOR, array($path, $class . ".php"));
     63       if (file_exists($file)) {
     64         require($file);
     65         break;
     66     }
     67   }
     68 
     69   // Read the contents of the FlatBuffer binary file.
     70   $filename = "monster.dat";
     71   $handle = fopen($filename, "rb");
     72   $contents = $fread($handle, filesize($filename));
     73   fclose($handle);
     74 
     75   // Pass the contents to `GetRootAsMonster`.
     76   $monster = \MyGame\Example\Monster::GetRootAsMonster($contents);
     77 ~~~
     78 
     79 Now you can access values like this:
     80 
     81 ~~~{.php}
     82   $hp = $monster->GetHp();
     83   $pos = $monster->GetPos();
     84 ~~~
     85 
     86 ## Text Parsing
     87 
     88 There currently is no support for parsing text (Schema's and JSON) directly
     89 from PHP.
     90