PHP code example of arzzen / php-flatbuffers

1. Go to this page and download the library: Download arzzen/php-flatbuffers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arzzen / php-flatbuffers example snippets


{
    "repositories": [
        {
            "url": "https://github.com/arzzen/php-flatbuffers.git",
            "type": "git"
        }
    ],
    "
ruby
use FlatBuffers\Table;
use FlatBuffers\FlatBufferBuilder;
use FlatBuffers\ByteBuffer;
use FlatBuffers\Constants;

class StringWrapper extends Table implements Constants
{
	
	private $fbb;
	
	public function __construct(FlatBufferBuilder $flatBufferBuilder)
	{
		$this->fbb = $flatBufferBuilder;
	}
	
	public function init(ByteBuffer $byteBuffer)
	{
		$this->bb = $byteBuffer;
		$this->bb_pos = $this->bb->getInt($this->bb->getPosition()) + $this->bb->getPosition();
		
		return $this;
	}
	
	public function getString($slot = 0)
	{
		$vtable_offset = self::SIZEOF_INT + ($slot * 2); 
		
		$vtable = $this->bb_pos - $this->bb->getInt($this->bb_pos);
		
		$offset = $vtable_offset < $this->bb->getShort($vtable) ? $this->bb->getShort($vtable + $vtable_offset) : 0;
		
		$offset += $this->bb_pos + $this->bb->getInt($offset + $this->bb_pos);
		$len = $this->bb->getInt($offset);
		$startPos = $offset + self::SIZEOF_INT;
		$_string = substr($this->bb->_buffer, $startPos, $len);

		return ($offset != 0 ? $_string : null);
	}
	
	public function createString($value)
	{
		return $this->fbb->createString($value);
	}
	
	public function addString($slot, $str)
	{
		$this->fbb->addOffsetX($slot, $str, 0);
	}

	public function dataBuffer()
	{
		return $this->fbb->dataBuffer();
	}
	
	public function startObject($numfields)
	{
		$this->fbb->startObject($numfields);
	}
	
	public function endObject()
	{
		return $this->fbb->endObject();
	}
	
	public function finish($root_table, $identifier = NULL)
	{
		$this->fbb->finish($root_table, $identifier);
	}
	
}


$flatBufferBuilder = new FlatBufferBuilder(1);
$stringWrapper = new StringWrapper($flatBufferBuilder);

// set string
$firstText = $stringWrapper->createString('first_value');
$secondText = $stringWrapper->createString('second_value');

$stringWrapper->startObject(25);
$stringWrapper->addString(2, $firstText);
$stringWrapper->addString(3, $secondText);
$stringWrapper->finish($stringWrapper->endObject());

// get string
$stringWrapper->init($stringWrapper->dataBuffer());

echo $stringWrapper->getString(2);
echo $stringWrapper->getString(3);