抽象方法 class 不使用由派生 class 声明的 属性
Method of an abstract class doesn't use a property, that was declared by derived class
我目前正在尝试通过 "notification" 模块扩展我自己的 WordPress-themes 小框架。这不应该取代异常——即使概念可能相似。由于我正在尝试编写框架 object-oriented 并且对整个概念仍然很陌生,所以我 运行 遇到了一个我不知道如何解决的问题。
以下代码 声明了一个抽象通知class,构造函数请求notification-title 和-message。之后,构造函数将成员方法show()
添加到wordpress动作栈中。由于用户和 framework-code 的其余部分不应将通知类型作为参数传递,因此我为每种通知类型创建了四个 classes,扩展了通知 class。那些 child classes 不需要覆盖显示通知的整个方法,他们只需要更改 class 它们的 type
属性正在延长。基本功能有效,因此通知显示在我的页面上。
问题是:当WordPress在page-footer上调用show()
时,type
属性是空的' do_action( 'ease_notifications' );
。我发现,在调用构造函数之前未设置 属性s(这在某种程度上确实有意义......)。无论如何,使用type
属性 的方法是show()
,并且在构造class 很久之后调用此方法。所以理论上, 属性 应该已经设置好了。你能给我解释一下为什么不是吗?
abstract class EaseNotification
{
private $type;
private $title;
private $message;
public function __construct( $title, $message )
{
$this->title = $title;
$this->message = $message;
add_action( 'ease_notifications', array(&$this, 'show') );
}
public function show()
{
echo '<div class="ease-notification type-' . $this->type . '">'
. '<span class="ease-notification-title">' . $this->title . '</span>'
. '<p class="ease-notification-body">' . $this->message . '</p>'
. '</div>';
}
}
class EaseError extends EaseNotification { private $type = "error"; }
class EaseWarning extends EaseNotification { private $type = "warning"; }
class EaseInformation extends EaseNotification { private $type = "information"; }
class EaseConfirmation extends EaseNotification { private $type = "confirmation"; }
通知的使用class:
require_once( 'class.easenotification.php' );
new EaseConfirmation( 'Hurray', 'It works, so you can be very happy.' );
你的 EaseNotification
class 中的 $type
属性 与你的 EaseConfirmation
中的 $type
属性 不一样] class。您已将它们声明为私有,因此它们都具有 class 它们仅在其中声明的范围/可见性。
有几种方法可以解决这个问题,假设您只需要一个 $type
变量。
例如:
- 声明 属性
protected
而不是私有的,并且不要在扩展 class 中声明 属性。相反,在子 classes 的构造函数中设置值。父项 class 的 protected
属性 在子项 class 中可见。
- 在您的
EaseNotification
class 中添加一个(public 或受保护...)getter 和 setter 方法,以便您可以访问 属性 来自扩展的 classes。您还应该从子 classes 中删除 属性 声明,因为您将不再使用 属性。
我目前正在尝试通过 "notification" 模块扩展我自己的 WordPress-themes 小框架。这不应该取代异常——即使概念可能相似。由于我正在尝试编写框架 object-oriented 并且对整个概念仍然很陌生,所以我 运行 遇到了一个我不知道如何解决的问题。
以下代码 声明了一个抽象通知class,构造函数请求notification-title 和-message。之后,构造函数将成员方法show()
添加到wordpress动作栈中。由于用户和 framework-code 的其余部分不应将通知类型作为参数传递,因此我为每种通知类型创建了四个 classes,扩展了通知 class。那些 child classes 不需要覆盖显示通知的整个方法,他们只需要更改 class 它们的 type
属性正在延长。基本功能有效,因此通知显示在我的页面上。
问题是:当WordPress在page-footer上调用show()
时,type
属性是空的' do_action( 'ease_notifications' );
。我发现,在调用构造函数之前未设置 属性s(这在某种程度上确实有意义......)。无论如何,使用type
属性 的方法是show()
,并且在构造class 很久之后调用此方法。所以理论上, 属性 应该已经设置好了。你能给我解释一下为什么不是吗?
abstract class EaseNotification
{
private $type;
private $title;
private $message;
public function __construct( $title, $message )
{
$this->title = $title;
$this->message = $message;
add_action( 'ease_notifications', array(&$this, 'show') );
}
public function show()
{
echo '<div class="ease-notification type-' . $this->type . '">'
. '<span class="ease-notification-title">' . $this->title . '</span>'
. '<p class="ease-notification-body">' . $this->message . '</p>'
. '</div>';
}
}
class EaseError extends EaseNotification { private $type = "error"; }
class EaseWarning extends EaseNotification { private $type = "warning"; }
class EaseInformation extends EaseNotification { private $type = "information"; }
class EaseConfirmation extends EaseNotification { private $type = "confirmation"; }
通知的使用class:
require_once( 'class.easenotification.php' );
new EaseConfirmation( 'Hurray', 'It works, so you can be very happy.' );
你的 EaseNotification
class 中的 $type
属性 与你的 EaseConfirmation
中的 $type
属性 不一样] class。您已将它们声明为私有,因此它们都具有 class 它们仅在其中声明的范围/可见性。
有几种方法可以解决这个问题,假设您只需要一个 $type
变量。
例如:
- 声明 属性
protected
而不是私有的,并且不要在扩展 class 中声明 属性。相反,在子 classes 的构造函数中设置值。父项 class 的protected
属性 在子项 class 中可见。 - 在您的
EaseNotification
class 中添加一个(public 或受保护...)getter 和 setter 方法,以便您可以访问 属性 来自扩展的 classes。您还应该从子 classes 中删除 属性 声明,因为您将不再使用 属性。