Joe*_*oey 14 php oop overloading
是否可以检查PHP中的子类是否已覆盖某个方法?
<!-- language: lang-php -->
class foo {
protected $url;
protected $name;
protected $id;
var $baz;
function __construct($name, $id, $url) {
$this->name = $name;
$this->id = $id;
$this->url = $url;
}
function createTable($data) {
// do default actions
}
}
Run Code Online (Sandbox Code Playgroud)
儿童班:
class bar extends foo {
public $goo;
public function createTable($data) {
// different code here
}
}
Run Code Online (Sandbox Code Playgroud)
迭代定义为此类成员的对象数组时,如何检查哪个对象具有新方法而不是旧方法?这样的功能是否method_overridden(mixed $object, string $method name)存在?
foreach ($objects as $ob) {
if (method_overridden($ob, "createTable")) {
// stuff that should only happen if this method is overridden
}
$ob->createTable($dataset);
}
Run Code Online (Sandbox Code Playgroud)
我知道模板方法模式,但是我想说我希望程序的控制与类和方法本身分开.我需要一个功能,method_overridden以实现这一目标.
nic*_*ass 23
检查声明类是否与对象的类匹配:
$reflector = new \ReflectionMethod($ob, 'createTable');
$isProto = ($reflector->getDeclaringClass()->getName() !== get_class($ob));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3318 次 |
| 最近记录: |