OOP 问题:不能调用抽象方法
OOP matter : Cannot call abstract method
我有一个 PHP 核心和一个抽象 class AppBase
,它使用一个特征 Uninstall
。
为了强制开发者实现一个静态函数来删除主 class MyApp
中的一些选项,AppBase
实现了一个带有静态函数的接口 'delete_options()'.
AppBase
abstract class AppBase implements iUninstall{
use Uninstall;
}
卸载
trait Uninstall{
public static function uninstall(){
//Do some general stuff
self::delete_options();
}
}
iUninstall
interface iUninstall {
public static function delete_options();
}
MyApp
include_once "core/iUninstall.php";
include_once "core/Uninstall.php";
include_once "core/AppBase.php";
class MyApp extends AppBase{
public static function delete_options() {
delete_option( "first-option" );
delete_option( "second-option" );
}
}
我的问题是出现了这个错误:
PHP Fatal error: Uncaught Error: Cannot call abstract method iUninstall::delete_options() in Uninstall.php
我可以看到特征 Uninstall
必须附加到 AppBase
才能使用 delete_options
所以我的 OOP 架构有问题。
我该如何解决这个问题?
首先,您应该得到一个关于 AppBase
具有抽象方法 delete_options()
而不是抽象方法 class 的致命错误。所以,你需要让AppBase
成为一个摘要class。 (但也许您只是忘记将其复制到您的示例中。)
然后,在 Uninstall::uninstall()
中,您需要使用 static
而不是 self
(以利用 late static binding)。
所以,总结一下:
trait Uninstall {
public static function uninstall(){
// static instead of self
static::delete_options();
}
}
interface iUninstall {
public static function delete_options();
}
// abstract class instead of class
abstract class AppBase implements iUninstall{
use Uninstall;
}
class MyApp extends AppBase {
public static function delete_options() {
echo 'deleting';
}
}
MyApp::uninstall();
/* result:
deleting
*/
或者...您可以将 delete_options()
作为 AppBase 中的(存根)方法实现,但您的问题中没有迹象表明这是您的初衷。
我有一个 PHP 核心和一个抽象 class AppBase
,它使用一个特征 Uninstall
。
为了强制开发者实现一个静态函数来删除主 class MyApp
中的一些选项,AppBase
实现了一个带有静态函数的接口 'delete_options()'.
AppBase
abstract class AppBase implements iUninstall{
use Uninstall;
}
卸载
trait Uninstall{
public static function uninstall(){
//Do some general stuff
self::delete_options();
}
}
iUninstall
interface iUninstall {
public static function delete_options();
}
MyApp
include_once "core/iUninstall.php";
include_once "core/Uninstall.php";
include_once "core/AppBase.php";
class MyApp extends AppBase{
public static function delete_options() {
delete_option( "first-option" );
delete_option( "second-option" );
}
}
我的问题是出现了这个错误:
PHP Fatal error: Uncaught Error: Cannot call abstract method iUninstall::delete_options() in Uninstall.php
我可以看到特征 Uninstall
必须附加到 AppBase
才能使用 delete_options
所以我的 OOP 架构有问题。
我该如何解决这个问题?
首先,您应该得到一个关于 AppBase
具有抽象方法 delete_options()
而不是抽象方法 class 的致命错误。所以,你需要让AppBase
成为一个摘要class。 (但也许您只是忘记将其复制到您的示例中。)
然后,在 Uninstall::uninstall()
中,您需要使用 static
而不是 self
(以利用 late static binding)。
所以,总结一下:
trait Uninstall {
public static function uninstall(){
// static instead of self
static::delete_options();
}
}
interface iUninstall {
public static function delete_options();
}
// abstract class instead of class
abstract class AppBase implements iUninstall{
use Uninstall;
}
class MyApp extends AppBase {
public static function delete_options() {
echo 'deleting';
}
}
MyApp::uninstall();
/* result:
deleting
*/
或者...您可以将 delete_options()
作为 AppBase 中的(存根)方法实现,但您的问题中没有迹象表明这是您的初衷。