title: PHP Initialize class variables without Constructor
body: <p>I have the following class,e.g.</p>
  
  <pre><code>class A{
   public var1;
   public var2;
  }
  </code></pre>
  
  <p>now, I want to create an object of this class, and I do the following:</p>
  
  <pre><code>$a = new A();
  $a-&gt;var1 = 123;
  $a-&gt;var2 = 987;
  </code></pre>
  
  <p>Is there a more better way to do this, since at times there can be even 20 such variables inside the class. I am just looking for compacting the code, and a constructor is not being used for this class. The values for variables are not static (that is why no static keyword is used)</p>
  
  <p>I am looking more for this kind of assignment we do for arrays:</p>
  
  <pre><code>$arr = array(
  'var1' =&gt; 123,
  'var2' =&gt; 987
  );
  </code></pre>
  
  <p>Furthermore, how will I go around avoiding NullPointerException (which sometimes comes up when I try to send this object to a .Net enabled web service) - This is optional though, as I can atlst relate this to null variables inside the class, but not exactly sure how to overcome this error.</p>