Hi,

Would be good to split complete different subjects to different threads
anyways:

On Wed, 2015-09-23 at 15:53 +0100, Dominic Grostate wrote:
> the
> ability to set default values of properties to instances of objects or
> calls to static methods or functions (expressions in general).

This causes questions on the order of execution and leads to waste of
CPU time in case the constructor overrides the default value while not
bringing much benefit over initialization in the constructor. It also is
complicated regarding exceptions being thrown.

Take the following code:

<?php

foo();

class A {
   protected $p1 = new Something();
   private $p2 = new SomethingElse($this->p1, $this->p3);
   private $p3 = new Something();
   private static $s1 = new StaticSomething();

   public function __construct() {}
}

bar();

class B extends A {
    private $p4 = new Something();

     public function __construct() {
         parent::__construct();
         $this->p1 = new Whatever();
     }
}

baz();

try {
    $b = new B();
} catch (Exception $e) {
}
?>

Some Questions:

      * Where, relative to foo(), bar() and baz() will StaticSomething's
        constructor be called? How do you capture exceptions from there?
      * When will $p2 be initialized, which properties can it access?
        (Only the ones initialized before? None?) 
      * In B's constructor we're overwriting $p1, so $p2 will be
        initialized with the wrong value.

I'm sure there are more questions ... doing this i the constructor makes
things clear and explicit with about the same amount of code to write.

johannes

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to