[英]Undefining a method or alternative
使用此插件系统,有一个实用程序和一个驱动程序。 该驱动程序扩展了该实用程序,但是,某些驱动程序支持的功能与其他驱动程序不同,某些驱动程序不支持某些功能。
我的课是这样的:
Class Utility {
public function MethodOne() {
# default MethodOne
}
public function MethodTwo() {
# default MethodTwo
}
}
class Driver extends Utility {
# MethodTwo not supported in this driver
}
取消定义方法的简单之处在于,它将允许使用该类的开发人员运行method_exists调用,以查看驱动程序是否支持该方法。
是否可以在扩展类中取消定义方法,还是有很好的选择?
您不能在子对象中取消定义父函数,这不是使用继承的好方法。 任何子类都应该能够使用所有父函数。 (否则,为什么还要麻烦?)
另一个解决方案是仅在每个特定驱动程序中定义所需的功能,而“实用工具”仅包含所有驱动程序都可以使用的通用功能(这是最简单的),然后在驱动程序内部包含特定于驱动程序的功能。
Class Utility {
public function MethodOne() {
# default MethodOne
}
public function MethodTwo() {
# default MethodTwo
}
}
class Driver extends Utility {
public function MethodThree() {
}
}
class Driver2 extends Utility {
public function MethodFour() {
}
}
这表明Driver和Driver2具有彼此不同的实现功能,同时仍然具有Utility中可用的方法。
最终的解决方案(如果您想强制将函数放入驱动程序中,否则会引发错误)将是实现多个接口,每个接口将一些函数捆绑在一起:
Class Utility {
public function MethodOne() {
print "MethodOne";
}
public function MethodTwo() {
print "MethodTwo";
}
}
interface inter1 {
public function MethodThree();
}
interface inter2 {
public function MethodFour();
}
class Driver extends Utility implements inter1 {
public function MethodThree(){
print "MethodThree";
}
}
class Driver2 extends Utility implements inter2 {
public function MethodFour() {
print "MethodFour";
}
}
两种解决方案都可以实现相同的目的,但是Driver必须在接口解决方案中实现MethodThree:
$d = new Driver();
print "Methods for Driver:\n";
foreach (array("MethodOne","MethodTwo","MethodThree","MethodFour") as $k) {
$p = method_exists($d, $k) ? 'true' : 'false';
print "\t ".$k.": " . $p ."\n";
}
$d = new Driver2();
print "Methods for Driver2:\n";
foreach (array("MethodOne","MethodTwo","MethodThree","MethodFour") as $k) {
$p = method_exists($d, $k) ? 'true' : 'false';
print "\t ".$k.": " . $p ."\n";
}
哪个输出:
Methods for Driver:
MethodOne: true
MethodTwo: true
MethodThree: true
MethodFour: false
Methods for Driver2:
MethodOne: true
MethodTwo: true
MethodThree: false
MethodFour: true
如果希望Driver2具有MethodThree,则应编写:
class Driver2 extends Utility implements inter2, inter1 {
public function MethodFour() {
print "MethodFour";
}
public function MethodThree() {
}
}
这个怎么样:
<?php
class Utility{
//will hold unimplemented methods foor every child
protected $unimplementedMethods = array();
protected function f1(){
//debug
echo __FUNCTION__ . "\n";
}
protected function f2(){
//debug
echo __FUNCTION__ . "\n";
}
//intercept the functions
public function __call($name, $arguments)
{
if (method_exists($this,$name) && !in_array($name, $this->unimplementedMethods)){
echo "Calling method: '$name'\n";
$this->$name();
}
else{
//return error or throw exception
echo "Method: '$name' is not implemented for this driver\n";
}
}
}
//driver1
class Driver1 extends Utility{
//set your non-implemented methods
protected $unimplementedMethods = array ('f2');
}
//driver2
class Driver2 extends Utility{
//set your non-implemented methods
protected $unimplementedMethods = array ('f1');
}
//init
$d1 = new Driver1();
$d2 = new Driver2();
$d1->f1(); //the ok method
$d1->f2(); //the nok method
$d2->f1(); //the nok method
$d2->f2(); //the ok method
?>
输出:
Calling method: 'f1'
Method: 'f2' is not implemented for this driver
Method: 'f1' is not implemented for this driver
Calling method: 'f2'
在面向对象的语言中,如果不扩大方法的范围,通常不能更改其可见性。
但是,尽管PHP不支持使用经典继承来限制方法的可见性,但事实证明,在特性方面,它允许这样做。
<?php
trait Utility {
public function methodOne() {
}
public function methodTwo() {
}
}
class Driver {
use Utility;
protected function methodTwo(){
}
}
现在我们开始:
php > $driver = new Driver;
php > $driver->methodTwo();
PHP Fatal error: Call to protected method Driver::methodTwo() from context '' in php shell code on line 1
警告:
无论可见性如何, method_exists
都将返回true。 您必须使用is_callable
:
php > var_dump(is_callable([$driver, 'methodOne']));
bool(true)
php > var_dump(is_callable([$driver, 'methodTwo']));
bool(false)
祝您项目顺利!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.