...

Text file src/github.com/google/flatbuffers/docs/source/PHPUsage.md

Documentation: github.com/google/flatbuffers/docs/source

     1Use in PHP    {#flatbuffers_guide_use_php}
     2==========
     3
     4## Before you get started
     5
     6Before diving into the FlatBuffers usage in PHP, it should be noted that
     7the [Tutorial](@ref flatbuffers_guide_tutorial) page has a complete guide to
     8general FlatBuffers usage in all of the supported languages
     9(including PHP). This page is specifically designed to cover the nuances of
    10FlatBuffers usage in PHP.
    11
    12You should also have read the [Building](@ref flatbuffers_guide_building)
    13documentation 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
    19The code for FlatBuffers PHP library can be found at `flatbuffers/php`. You
    20can browse the library code on the [FlatBuffers
    21GitHub page](https://github.com/google/flatbuffers/tree/master/php).
    22
    23## Testing the FlatBuffers JavaScript library
    24
    25The code to test the PHP library can be found at `flatbuffers/tests`.
    26The test code itself is located in [phpTest.php](https://github.com/google/
    27flatbuffers/blob/master/tests/phpTest.php).
    28
    29You 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
    37example of how to use FlatBuffers in PHP.*
    38
    39FlatBuffers supports both reading and writing FlatBuffers in PHP.
    40
    41To use FlatBuffers in your own code, first generate PHP classes from your schema
    42with the `--php` option to `flatc`. Then you can include both FlatBuffers and
    43the generated code to read or write a FlatBuffer.
    44
    45For example, here is how you would read a FlatBuffer binary file in PHP:
    46First, include the library and generated code (using the PSR `autoload`
    47function). Then you can read a FlatBuffer binary file, which you
    48pass 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
    79Now you can access values like this:
    80
    81~~~{.php}
    82  $hp = $monster->GetHp();
    83  $pos = $monster->GetPos();
    84~~~
    85
    86## Text Parsing
    87
    88There currently is no support for parsing text (Schema's and JSON) directly
    89from PHP.

View as plain text