title: PHP Class Property Name
body: <p>I have a class called contact:</p>
  
  <pre><code>class contacts
  {
      public $ID;
      public $Name;
      public $Email;
      public $PhoneNumber;
      public $CellPhone;
      public $IsDealer;
      public $DealerID;
  }
  </code></pre>
  
  <p>At some point in my code I would like to point to a property within that class and return the name of the property.</p>
  
  <pre><code>&lt;input type="text" 
     id="&lt;?php key($objContact-&gt;Name)" ?&gt;"
     name="&lt;?php key($objContact-&gt;Name)" ?&gt;"
     value="&lt;?php $_POST['contact'.key($objContact-&gt;Name)]" /&gt;
  </code></pre>
  
  <p>My issue being that the key() function only deals with arrays or objects. $objContact->Name itself does not meet these requirements. I know it would be just as simple to type the name itself out into the ID and NAME fields but this is for other code verification uses. Imagine the processor page:</p>
  
  <pre><code>$objContact = new contact();
  
  $objContact-&gt;Email = $_POST[$objContact-&gt;Email-&gt;**GetSinglePropertyName()**];
  
  $objContact-&gt;PhoneNumber = $_POST[$objContact-&gt;PhoneNumber-&gt;**GetSinglePropertyName()**];
  </code></pre>
  
  <p>This allows me to turn on STRICT and ensure that as I'm writing I'm not creating any fat finger errors along the way that are going to have me denting my head anymore than it presently exist.</p>
  
  <p><strong>UPDATE WITH ANSWER</strong>
  Answer provided by: <a href="https://stackoverflow.com/users/383007/linepogl">linepogl</a></p>
  
  <p>Now I've taken linepogl's idea and extended is some so it can work very easily with my existing code base. Here's what I've come up with:</p>
  
  <pre><code>class baseData {
      public $meta;
  
      public function __construct() {
          $this-&gt;meta = new Meta($this);
      }
  }
  
  class Meta {
    public function __construct($obj) {
      $a = get_object_vars($obj);
      foreach ($a as $key =&gt; $value){
        $this-&gt;$key = $key;   
      }
    }
  }
  
  class contacts extends baseData
  {
      public $ID;
      public $Name;
      public $Email;
      public $PhoneNumber;
      public $CellPhone;
      public $IsDealer;
      public $DealerID;
  }
  </code></pre>
  
  <p>Which means I can now call the following code with the desired results:</p>
  
  <pre><code>$objContact = new contacts();
  echo($objContact-&gt;meta-&gt;Email);
  </code></pre>