Hello Robin,

Thanks for your reply :-)

The behaviour of "static fields are shared between subclasses" is exactly
what I would like to question, since PHP is not a so called "static
programming language" like C++, Java and C#. Currently PHP's object model is
designed and implemented as class-based, the theory base of which is "Every
object is an instance of a class". However due to PHP interpreter's running
model, classes' lifetime is limited into a HTTP request's life cycle, which
is different from traditional classes' lifetime in C++, Java, C#, or even
Python, whose classes' lifetime is as long as the worker process, and has
nothing to do with HTTP request. From this perspective, PHP's class is not
traditional meaning of class, or in other words, PHP's class is simply an
object in static programming language such as C++, Java, C#. So I think
treating PHP's class as an object is very natural.

A common new Web 2.0 PHP Framework's design pattern is "router pattern",
which resolves URI to controller and action. As far as I know, the framework
developers usually write a Router class, and then instanciates a $router
object, call its route() method to get the controller/action pair. When the
request finishes, the class' properties is totally destroyed. This brings me
a thought: Why don't use the Router class as an object directly since the
life cycle of the class is the same as the object? Why we must "new" an
Router class to get a router object and then use it? Why we use Singleton
but not the class itself, even when we know that PHP has no userland
threading synchronization problems?

My idea is that a class in PHP could be treated as an object. Then, objects
should support inheritance (for example, there are RewritedRouter object and
NormalRouter object which extends Router "abstract object"), then the
question occurs: static fields in PHP do not follow dynamic property's
inheritance rule, and abstract static function is deprecated (but is allowed
in interfaces, why?), which means this programming diagram is not officially
supported.

Since PHP 5.3 introduces late static binding, so is there any chance that
"static::" under static context behaves like $this under dynamic context?
Then the codes will be a little shorter and meaningful:

<?php
class static_a {
   public static function change($name) {
       static::$name = $name;
    }
   public static $name = 'a';
}
class static_c extends static_a {}
class static_d extends static_a {}

echo static_a::$name; // a
static_c::change('c');
echo static_a::$name; // a
static_d::change('d');
echo static_a::$name; // a
?>

Thank you all very much :-)

2009/5/18 Robin Fernandes <rewbs.s...@gmail.com>

> 2009/5/16 Jingcheng Zhang <dio...@gmail.com>:
> >
> > Maybe I have not found its detailed description on PHP's official manual,
> > but PHP does allow static field inheritance. However there is a little
> > difference between dynamic field inheritance and static field
> inheritance,
> > as the following codes shows:
>
> Hi!
>
> I think the current behaviour is as expected.
> In that example, there is a single static field which is shared
> between classes static_a, static_b and static_c. Because there is just
> one value of $name, the class through which you access the value makes
> no difference. On the other hand, in the case of classes dynamic_a,
> dynamic_b and dynamic_c, by the nature of instance properties, each
> instance has its own value of $name. So changing the value in one
> instance does not affect the others. This behaviour is similar to many
> other OO languages, including Java, C++ and C#.
>
> Perhaps a closer equivalent of the Javascript code you provided is as
> follows, where the static field is re-declared in subclasses, and
> therefore exists as a separate entity in each class. Again, this
> distinction between inherited static fields and "re-declared" static
> fields also applies to other languages such as Java, C++ and C#.
>
> <?php
> class static_a {
>    public static function change($name) {
>         static::$name = $name;
>     }
>    public static $name = 'a';
> }
> class static_c extends static_a {
>     public static $name;
> }
> class static_d extends static_a {
>     public static $name;
> }
>
> echo static_a::$name; // a
> static_c::change('c');
> echo static_a::$name; // a
> static_d::change('d');
> echo static_a::$name; // a
> ?>
>
> What do you think?
>
> Regards,
> Robin
>



-- 
Best regards,
Jingcheng Zhang
P.R.China

Reply via email to