I'm not able to use getters and setters of Types.php generated by Thrift.
class Creation {
static $_TSPEC;
public $a_iso = null;
public $date = null;
public function __construct($vals=null) {
if (!isset(self::$_TSPEC)) {
self::$_TSPEC = array(
1 => array(
'var' => 'a_iso',
'type' => TType::I32,
),
2 => array(
'var' => 'date',
'type' => TType::I32,
),
);
}
if (is_array($vals)) {
if (isset($vals['a_iso'])) {
$this->a_iso = $vals['a_iso'];
}
if (isset($vals['date'])) {
$this->date = $vals['date'];
}
}
}
public function read($input)
{
$xfer = 0;
$fname = null;
$ftype = 0;
$fid = 0;
$xfer += $input->readStructBegin($fname);
while (true)
{
$xfer += $input->readFieldBegin($fname, $ftype, $fid);
if ($ftype == TType::STOP) {
break;
}
switch ($fid)
{
case 1:
if ($ftype == TType::I32) {
$xfer += $input->readI32($this->a_iso);
} else {
$xfer += $input->skip($ftype);
}
break;
case 2:
if ($ftype == TType::I32) {
$xfer += $input->readI32($this->date);
} else {
$xfer += $input->skip($ftype);
}
break;
default:
$xfer += $input->skip($ftype);
break;
}
$xfer += $input->readFieldEnd();
}
$xfer += $input->readStructEnd();
return $xfer;
}
public function write($output) {
$xfer = 0;
$xfer += $output->writeStructBegin('Creation');
if ($this->a_iso !== null) {
$xfer += $output->writeFieldBegin('a_iso', TType::I32, 1);
$xfer += $output->writeI32($this->a_iso);
$xfer += $output->writeFieldEnd();
}
if ($this->date !== null) {
$xfer += $output->writeFieldBegin('date', TType::I32, 2);
$xfer += $output->writeI32($this->date);
$xfer += $output->writeFieldEnd();
}
$xfer += $output->writeFieldStop();
$xfer += $output->writeStructEnd();
return $xfer;
}
}
I did :
$objetcree = new Creation();
$objetcree->a_iso = 45;
Ok but I don't want use like that.
$objetcree->read($input);
How has to be $input if I want to write just the a_iso field ?
My Thrift structure :
typedef i32 int
struct Creation {
1: int a_iso,
2: int date
}
Thank you!
Input
is expected to be an instance of TProtocol
which delivers the data from the input stream.
When you only want to set a simple value at your object instance, there's no point in using read()
unless you have such an input stream. All else would be big time overkill.
Ok but I don't want use like that
But, well, it works this way. You don't have to actually like it.
How has to be $input if I want to write just the a_iso field?
Just for the sake of completeness: The format depends primarily on the protocol you want to use. You could use binary, JSON and a bunch of other formats. Depending on the transport, there may some more data needed. But again, it does not make sense this way, except you are not telling the whole story why you want to do it in such an overly complicated manner.
I have many xml files. I would like to store them sustainably [...] XML_file -> object -> thrift_object -> binary_file
Ok, now it starts to make sense. I'm not a PHP Pro and I don't know much about the details, so I can give you only the outline. But that should give a good starting point. Theoretically.
Load the XML file and deserialize the data into some kind of object structure. At this stage, Thrift can't help you much as a) there is no Thrift XML protocol, and b) there is no "the" XML format - XML basically describes only the syntax of valid XML documents, not what's in a concrete XML instance. The latter is highly variable and usually defined due to DTDs or XML Schema. Although there are some similarities between an XML Schema or DTD to a Thrift IDL, they do not really have anything in common.
If the XML parser framework of your choice is spitting out fully instantiated objects, you will have to convert them into Thrift objects. If the parser is more low-level and gives you data on a path, properties and values level, you can directly construct the Thrift objects during that phase. In either case you take the Thrift-generated code, create an instance of the desired target class and set all the properties as needed.
Once you did read and convert enough or all data into Thrift objects, you use the TBinarySerializer
class and call the serialize()
method, like $buffer = $serializer.serialize($object)
. The returned $buffer
contains the data serialized in binary format.
The TBinarySerializer
class can be found under /lib/php/src/lib/Thrift/Serializer