Hello all,
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:
<?php
class static_a {
public static function change($name) {
self::$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; // c
static_d::change('d');
echo static_a::$name; // d
class dynamic_a {
public function change($name) {
$this->name = $name;
}
public $name = 'a';
}
class dynamic_c extends dynamic_a {}
class dynamic_d extends dynamic_a {}
$a = new dynamic_a();
$c = new dynamic_c();
$d = new dynamic_d();
echo $a->name; // a
$c->change('c');
echo $a->name; // a
$d->change('d');
echo $a->name; // a
?>
The result of static inheritance test can be meaningful on some
way(especially on class-based programming perspective), but when considering
the static class as "object" in prototype-based programming(which I doubt is
some people's favourite who comes from prototype-based OOP community), this
result can be confusing. On JavaScript, this example can be:
This looks more meaningful. So my suggestion is, could PHP's static
inheritance rule follow the dynamic inheritance rule ( $this on dynamic, or
prototype delegation on JavaScript) ?
Thanks :-)
--
Best regards,
Jingcheng Zhang
P.R.China
Nobody is interested in this topic? Another confusing feature is "static
function", which is allowed in interface, but not allowed in abstract class,
any explanation or reason for this? Thanks :-)
2009/5/16 Jingcheng Zhang diogin@gmail.com
Hello all,
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:<?php
class static_a {
public static function change($name) {
self::$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; // c
static_d::change('d');
echo static_a::$name; // dclass dynamic_a {
public function change($name) {
$this->name = $name;
}
public $name = 'a';
}
class dynamic_c extends dynamic_a {}
class dynamic_d extends dynamic_a {}$a = new dynamic_a();
$c = new dynamic_c();
$d = new dynamic_d();
echo $a->name; // a
$c->change('c');
echo $a->name; // a
$d->change('d');
echo $a->name; // a
?>The result of static inheritance test can be meaningful on some
<script type="text/javascript"> function extends(parent) { var T = function () {}; T.prototype = parent; return new T(); } var static_a = { name: 'a', change: function (name) { this.name = name; } }; var static_c = extends(static_a); var static_d = extends(static_a); alert(static_a.name); // a static_c.change('c'); alert(static_a.name); // a static_d.change('d'); alert(static_a.name); // a </script>
way(especially on class-based programming perspective), but when considering
the static class as "object" in prototype-based programming(which I doubt is
some people's favourite who comes from prototype-based OOP community), this
result can be confusing. On JavaScript, this example can be:This looks more meaningful. So my suggestion is, could PHP's static
inheritance rule follow the dynamic inheritance rule ( $this on dynamic, or
prototype delegation on JavaScript) ?
Thanks :-)--
Best regards,
Jingcheng Zhang
P.R.China
--
Best regards,
Jingcheng Zhang
Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University
P.R.China
2009/5/16 Jingcheng Zhang diogin@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
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.soal@gmail.com
2009/5/16 Jingcheng Zhang diogin@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