Re: Implementing Generics, and none scalar default properties.
| From: | Dominic Grostate | Date: | Fri, 25 Sep 2015 15:13:44 +0000 |
| Subject: | Re: Implementing Generics, and none scalar default properties. | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88497@lists.php.net to get a copy of this message | ||
Hi,
I see what you mean with static properties, that would be more difficult.
I can answer on non-static however.
* When will $p2 be initialized, which properties can it access?
- On class instantiation. It won't have access to $this, because the
object won't have been created yet. It will have access to the same
entities as a static method.
* In B's constructor we're overwriting $p1, so $p2 will be initialized with
the wrong value.
- It won't have access to $this. In effect they are pre-constructor
assignments, just like any other property.
Concerning reassignment on constructor leading to wasted code, I can only
place faith in the effectiveness of the user to override those properties
to NULL.
Anyway sorry about the mixed post. I've mainly been working on
implementing generics, so I have a separate thread somewhere for that.
Dominic
On 25 Sep 2015 1:09 pm, "Johannes Schlüter" <johannes@schlueters.de> wrote:
> 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
>
>