title: How to serialize an "Object" type in PHP to be sent to a WCF Service?
body: <p>My WCF Service can accept a request to update a field dynamically by passing the value wrapped in an Object and a DynamicAttributeType enum to specify the type, like so:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[DataContract]
  [KnownType(typeof(string))]
  [KnownType(typeof(bool))]
  [KnownType(typeof(PickListInfo))]
  public class DynamicAttributeInfo
  {
    [DataMember]
    public string Name { get; set; }
  
    [DataMember]
    public object Value { get; set; }
  
    [DataMember]
    public DynamicAttributeType DynamicAttributeType { get; set; }
  }
  </code></pre>
  
  <p>This works fine for .Net clients. I'm trying to help a PHP client make it work. So far we're having trouble with creating something that will pass as Object but still contain the true value inside (string/bool/PickList).</p>
  
  <p><strong>Please note:</strong> the WCF service is in production and changing it is not an option. I'm looking for a solution for the PHP client. </p>
  
  <p>Here are his different attempts:</p>
  
  <p>1) First try - simple string as value:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
     $da = new DynamicAttributeInfo();
     $da-&gt;DynamicAttributeType = 'String';
     $da-&gt;Name = 'lv_eutmsource';
     $da-&gt;Value = 'test';
  ?&gt;
  </code></pre>
  
  <p>exception:
   [InnerException] => stdClass Object
                                   (
                                       [HelpLink] =>
                                       [InnerException] =>
                                       [Message] => End element 'Value' from namespace 'http://schemas.datacontract.org/2004/07/XXX.XXX.XXXService.Common.Entities' expected. Found text 'test'. Line 2, position 725.</p>
  
  <p>2) Second take (semi-successful): a new object of empty class</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
   class testText() {
   }
  
   $da = new DynamicAttributeInfo();
   $da-&gt;DynamicAttributeType = 'String';
   $da-&gt;Name = 'lv_eutmsource';
   $da-&gt;Value = new testText();
  ?&gt;
  </code></pre>
  
  <p>There was no WCF exception this time, but no value was actually passed and my code ended up with "Object.ToString".</p>
  
  <p>3) Other attempts: non-empty classes - he tried to add fields with different names - nothing worked.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
   class Value() {
       public $String;
   }
  
   $text = new Value();
   $text-&gt;String = 'test text';
  
   $da = new DynamicAttributeInfo();
   $da-&gt;DynamicAttributeType = 'String';
   $da-&gt;Name = 'lv_eutmsource';
   $da-&gt;Value = $text;
  ?&gt;
  </code></pre>
  
  <p>exception:
   [InnerException] => stdClass Object
                           (
                               [HelpLink] =>
                               [InnerException] => stdClass Object
                                   (
                                       [HelpLink] =>
                                       [InnerException] =>
                                       [Message] => End element 'Value' from namespace 'http://schemas.datacontract.org/2004/07/XXX.XXX.XXXService.Common.Entities' expected. Found element 'String' from namespace ''.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
   class textText() {
       public $text;
   }
  
   $text = new testText();
   $text-&gt;text = 'test text';
  
   $da = new DynamicAttributeInfo();
   $da-&gt;DynamicAttributeType = 'String';
   $da-&gt;Name = 'lv_eutmsource';
   $da-&gt;Value = $text;
  ?&gt;
  </code></pre>
  
  <p>exception:
   [InnerException] => stdClass Object
                           (
                               [HelpLink] =>
                               [InnerException] => stdClass Object
                                   (
                                       [HelpLink] =>
                                       [InnerException] =>
                                       [Message] => End element 'Value' from namespace 'http://schemas.datacontract.org/2004/07/XXX.XXX.XXXService.Common.Entities' expected. Found element 'text' from namespace ''.</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
   class Value() {
       public $Value;
   }
  
   $text = new Value();
   $text-&gt;Value = 'test text';
  
   $da = new DynamicAttributeInfo();
   $da-&gt;DynamicAttributeType = 'String';
   $da-&gt;Name = 'lv_eutmsource';
   $da-&gt;Value = $text;
  ?&gt;
  </code></pre>
  
  <p>exception:
   [InnerException] => stdClass Object
                           (
                               [HelpLink] =>
                               [InnerException] => stdClass Object
                                   (
                                       [HelpLink] =>
                                       [InnerException] =>
                                       [Message] => End element 'Value' from namespace 'http://schemas.datacontract.org/2004/07/XXX.XXX.XXXService.Common.Entities' expected. Found element 'Value' from namespace ''. </p>