瀏覽代碼

Adds fluent setters for PHP (#3130)

Brent Shaffer 8 年之前
父節點
當前提交
4d5daf4ef9
共有 2 個文件被更改,包括 15 次插入0 次删除
  1. 13 0
      php/tests/generated_class_test.php
  2. 2 0
      src/google/protobuf/compiler/php/php_generator.cc

+ 13 - 0
php/tests/generated_class_test.php

@@ -877,4 +877,17 @@ class GeneratedClassTest extends TestBase
         $m = new \Foo\PBEmpty();
         $m = new \PrefixEmpty();
     }
+
+    #########################################################
+    # Test fluent setters.
+    #########################################################
+
+    public function testFluentSetters()
+    {
+        $m = (new TestMessage())
+            ->setOptionalInt32(1)
+            ->setOptionalInt64(2);
+        $this->assertSame(1, $m->getOptionalInt32());
+        $this->assertSame(2, $m->getOptionalInt64());
+    }
 }

+ 2 - 0
src/google/protobuf/compiler/php/php_generator.cc

@@ -530,6 +530,8 @@ void GenerateFieldAccessor(const FieldDescriptor* field, bool is_descriptor,
         "field_name", field->name());
   }
 
+  printer->Print("\nreturn $this;\n");
+
   Outdent(printer);
 
   printer->Print(