> 
> What about inheritance?
> I think dynamic class-constructor would make much more sense.
> A function which can analyse real class and do initialisation.
> 
> class A
> {
>     protected static function __class_construct()
>     {
>         echo get_called_class().” class is defined\n";
>     }
> }
> 
> class B extends A
> {
> }
> 
> #### output ####
> A class is defined
> B class is defined
> 

I think class-constructor and static-constructor are two different
things. I even think that a inherited class-constructor is a dangerous
thing because you have huge depencies between all subclasses.

For a quick and dirty example:

abstract class A {
    protected static $value;

    protected static function __class_construct() {
        switch (get_called_class()):
           case 'B':
                self::$value = 1;
                break;
           case 'C':
                self::$value = 2;
                break;
           default:
                self::$value = 3;
    }
}

class B extends A {
    public static function isOne() {
        return self::$value == 1;
    }

}

class C extends A {
    public static function isTwo() {
        return self::$value == 2;
    }
}

That not wellformed code for three reasons:

1. Class A must know about all of it's subclasses, so you can not easily
reuse this class A in other projects because you have to refactor the
complete __class_constructor.

2. Huge dependcies between all subclasses. If you want to implement a
class D although extending class A you have to look in each other class
for sideeffects of the the changes you have todo in the
__class_construct for this class D

3. As you can see in the quick example you have a depency of class
loading order as well the result of B::isOne() and C::isTwo() depends in
which order you have loaded your classfiles. Even worst for example
You are using only class B for the beginn B::isOne() giving you true,
later  you load class C for another reason and suddenly B::isOne() gives
you false.

A static constructor however encapsulate the initialization to each
class and should only initialize private members which can be accessed
by protected getters by the subclass.

For the example:


abstract class A {
    private static $one;

    private static function __static() {
        self::$one = 1;
    }

    protected static function getOne() {
        return self::$one;
    }
}

class B extends A {
    public static function isOne() {
        return self::getOne() == 1; //Now always true
    }

}

class C extends A {
    public static function isTwo() {
        return self::getOne() == 2; //Now always false
    }
}

Regards,

-- 
DerOetzi

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to