自我和静态之间的地面?
我正在寻找SELF和STATIC之间的中间地带,没有出现Late Static Binding的意外行为。
以下是我的代码,包含练习结果和预期结果:
<?php
class A {
public function get_self() {
return new self();
}
public function get_static() {
return new static();
}
}
class B extends A {}
class C {
public function static_get_a_self() {
return A::get_self();
}
public function static_get_a_static() {
return A::get_static();
}
public function var_get_a_self() {
$a = new A();
return $a->get_self();
}
public function var_get_a_static() {
$a = new A();
return $a->get_static();
}
public function static_get_b_self() {
return B::get_self();
}
public function static_get_b_static() {
return B::get_static();
}
public function var_get_b_self() {
$b = new B();
return $b->get_self();
}
public function var_get_b_static() {
$b = new B();
return $b->get_static();
}
}
$a = new A();
$b = new B();
$c = new C();
echo "Static calls: n";
echo "B::get_self() ============= " . get_class(B::get_self()) . " | expected A n";
echo "B::get_static() =========== " . get_class(B::get_static()) . " | expected B n";
echo "A::get_static() =========== " . get_class(A::get_static()) . " | expected A n";
echo "n";
echo "Object Calls on A: n";
echo '$a->get_self() ============ ' . get_class($a->get_self()) . " | expected A n";
echo '$a->get_static() ========== ' . get_class($a->get_static()) . " | expected A n";
echo "n";
echo "Object Calls on B: n";
echo '$b->get_self() ============ ' . get_class($b->get_self()) . " | expected A n";
echo '$b->get_static() ========== ' . get_class($b->get_static()) . " | expected B n";
echo "n";
echo "C Object Calls on A: n";
echo "Internally against A::{call}n";
echo '$c->static_get_a_self() === ' . get_class($c->static_get_a_self()) . " | expected A n";
echo '$c->static_get_a_static() = ' . get_class($c->static_get_a_static()) . " | expected A < Whoa! n";
echo "Internally against $a = new A();n";
echo '$c->var_get_a_self() ====== ' . get_class($c->var_get_a_self()) . " | expected A n";
echo '$c->var_get_a_static() ==== ' . get_class($c->var_get_a_static()) . " | expected A n";
echo "n";
echo "C Object Calls on B: n";
echo "Internally against B::{call}n";
echo '$c->static_get_b_self() === ' . get_class($c->static_get_b_self()) . " | expected A n";
echo '$c->static_get_b_static() = ' . get_class($c->static_get_b_static()) . " | expected B < Whoa! n";
echo "Internally against $b = new B();n";
echo '$c->var_get_b_self() ====== ' . get_class($c->var_get_b_self()) . " | expected A n";
echo '$c->var_get_b_static() ==== ' . get_class($c->var_get_b_static()) . " | expected B n";
?>
结果:
Static calls:
B::get_self() ============= A | expected A
B::get_static() =========== B | expected B
A::get_static() =========== A | expected A
Object Calls on A:
$a->get_self() ============ A | expected A
$a->get_static() ========== A | expected A
Object Calls on B:
$b->get_self() ============ A | expected A
$b->get_static() ========== B | expected B
C Object Calls on A:
Internally against A::{call}
$c->static_get_a_self() === A | expected A
$c->static_get_a_static() = C | expected A < Whoa!
Internally against $a = new A();
$c->var_get_a_self() ====== A | expected A
$c->var_get_a_static() ==== A | expected A
C Object Calls on B:
Internally against B::{call}
$c->static_get_b_self() === A | expected A
$c->static_get_b_static() = C | expected B < Whoa!
Internally against $b = new B();
$c->var_get_b_self() ====== A | expected A
$c->var_get_b_static() ==== B | expected B
这种情况很有意义,在后期静态绑定中,保留的“静态”变量引用了最后一个类的作用域。 我在寻找的是一个保留变量,它指的是发生变量的顶级类。
IE:如果我用一个名为Member的类来扩展一个RecordObject类,我需要一个我可以在RecordObject中定义的函数来实例化一个新的成员(或Page或Comment),而不必在每个类定义中覆盖这个函数。 这可能吗?
我正在运行的问题是,例如,我在Member中,并且我调用Page :: get($ id),它在内部创建一个新对象并执行一些魔术。 不幸的是,如果我使用静态,那个新对象是一个成员...没有骰子。
你为什么忽视严格的错误?
只需将static
关键字添加到静态方法。
class A {
static public function get_self() {
return new self();
}
static public function get_static() {
return new static();
}
}
一切都应该如预期。
http://3v4l.org/RWRE9#v530
链接地址: http://www.djcxy.com/p/58055.html上一篇: ground between self and static?
下一篇: PHP 5.2 Equivalent to Late Static Binding (new static)?