All thrift PHP class constructors can take an array as the constructor argument
authorMark Slee <mcslee@apache.org>
Fri, 10 Nov 2006 23:07:35 +0000 (23:07 +0000)
committerMark Slee <mcslee@apache.org>
Fri, 10 Nov 2006 23:07:35 +0000 (23:07 +0000)
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

compiler/cpp/src/generate/t_php_generator.cc

index 5629b73..37c87f9 100644 (file)
@@ -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);