From: Mark Slee Date: Fri, 10 Nov 2006 23:07:35 +0000 (+0000) Subject: All thrift PHP class constructors can take an array as the constructor argument X-Git-Tag: 0.2.0~1601 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=19e71c34b190489e4ab261a69f206943a7dc56b0;p=common%2Fthrift.git All thrift PHP class constructors can take an array as the constructor argument Summary: It will init any field with the value in the array. Hot for converting DB rows into thrift objs. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664871 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc index 5629b73f..37c87f96 100644 --- a/compiler/cpp/src/generate/t_php_generator.cc +++ b/compiler/cpp/src/generate/t_php_generator.cc @@ -158,6 +158,27 @@ void t_php_generator::generate_php_struct_definition(ofstream& out, out << endl; + out << + indent() << "public function __construct($vals=null) {" << endl; + indent_up(); + out << + indent() << "if (is_array($vals)) {" << endl; + indent_up(); + for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { + out << + indent() << "if (isset($vals['" << (*m_iter)->get_name() << "'])) {" << endl << + indent() << " $this->" << (*m_iter)->get_name() << " = $vals['" << (*m_iter)->get_name() << "'];" << endl << + indent() << "}" << endl; + } + indent_down(); + out << + indent() << "}" << endl; + + indent_down(); + out << + indent() << "}" << endl << + endl; + generate_php_struct_reader(out, tstruct); generate_php_struct_writer(out, tstruct);