如果到现在我对ststic有点了解现在我意识到我什么都不懂.我很困惑,我很难理解,我不能.有人可以在使用self,parent,static时解释这个程序,以及我所做的所有最小的改变都会改变结果而不会让我无法理解正在发生的事情.非常感谢 ..
来自http://docs.php.net/language.oop5.late-static-bindings的代码
<?php
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}
C::test();
?>
Run Code Online (Sandbox Code Playgroud)
出局是:
A
C
C
Run Code Online (Sandbox Code Playgroud) 请参阅Web上有关PHP Factory Pattern的示例.
在线$kind =& Vehicle::category($wheel);
,我为什么要使用&
?
代码:
<?php
class Vehicle {
function category($wheel = 0) {
if ($wheel == 2) {
return "Motor";
} elseif($wheel == 4) {
return "Car";
}
}
}
class Spec {
var $wheel = '';
function Spec($wheel) {
$this->wheel = $wheel;
}
function name() {
$wheel = $this->wheel;
return Vehicle::category($wheel);
}
function brand() {
$wheel = $this->wheel;
$kind =& Vehicle::category($wheel);
if ($kind == "Motor") {
return array('Harley','Honda');
} elseif($kind = "Car") …
Run Code Online (Sandbox Code Playgroud)