title: What is the difference between assigning property values using a constructor and property assignment in the class declaration?
body: <p>What is the difference between assigning property values using a constructor and direct property assignment within the class declaration? In other words, what is the difference between the following two pieces of code making default values for the new object?</p>
  
  <p>Code with direct assignment:</p>
  
  <pre><code>&lt;?php
   class A {
     public $name="aName";
     public $weight = 80;
     public $age = 25;
     public $units = 0.02 ;
   }
  ?&gt;
  </code></pre>
  
  <p>Code with constructor:</p>
  
  <pre><code>&lt;?php
   class A {
     public $name;
     public $weight;
     public $age;
     public $units;
     public function __construct() {
         $this-&gt;name = "aName";
         $this-&gt;weight = 80;
         $this-&gt;age = 25;
         $this-&gt;units= 0.02 ;
     }
   }
  ?&gt;
  </code></pre>
  
  <p>You may answer that i can't change the hard coded properties, but i could in the following code( In Local Sever ):</p>
  
  <pre><code>&lt;?php
    class A{
       public $name="aName";
       public $weight = 80;
       public $age = 25;
       public $units = 0.02 ;
    }
   class B extends A{
      public function A_eat(){
         echo $this-&gt;name.' '."is".' '.$this-&gt;age.' '."years old&lt;br&gt;";
         echo $this-&gt;name.' '."is eating".' '.$this-&gt;units.' '."units of food&lt;br&gt;";
         $this-&gt;weight +=$this-&gt;units;
         echo $this-&gt;name.' '."weighs".' '.$this-&gt;weight."kg";
       }
     }
    $b = new B();
    echo "&lt;p&gt;If no changes to the object's Properties it inherits the main class's&lt;/p&gt;";
    $b-&gt;A_eat();
    echo '&lt;br&gt;&lt;br&gt;';
    echo "&lt;p&gt;If changes made to the object's Properties it uses it's new properties&lt;/p&gt;";
    $b-&gt;name ="bName";
    $b-&gt;weight = 90;
    $b-&gt;units = 0.05;
    $b-&gt;A_eat();
  ?&gt;
  </code></pre>