I am currently trying to extend my own little framework for WordPress-themes by a “notification” module. This is not supposed to replace exceptions – even though the concept might be similar. Since I am trying to write the framework object-oriented and am still new to this whole concept, I ran into a problem which I can’t figure out how to solve.
The following code declares an abstract notification class with a constructor requesting notification-title and -message. Afterwards, the constructor adds the member method show()
to the wordpress action stack. Since the user and the rest of the framework-code should not pass the type of the notification as a parameter, I created four classes for every notification type, extending the notification class. Those child classes don’t need to overwrite the whole method of showing the notification, they only need to change the type
property of the class they are extending. The base functionality works, so the notifications are shown on my page.
The problem is: The type
property is empty, when show()
is called on the page-footer by WordPress’ do_action( 'ease_notifications' );
. I found out, that propertys are not set before the constructor is called (which really makes sense somehow…). Anyway, the method using the type
property is show()
, and this method is called long after the class has been constructed. So theoretically, the property should have been set by then. Can you explain to me why it isn’t?
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"; }
Usage of the notification class:
require_once( 'class.easenotification.php' );
new EaseConfirmation( 'Hurray', 'It works, so you can be very happy.' );
The
$type
property in yourEaseNotification
class is not the same as the$type
property in yourEaseConfirmation
class. You have declared them private so both have the scope / visibility of the class they are declared in only.There are a few ways you can solve that, assuming that you want one
$type
variable only.For example:
protected
instead of private and don’t declare the property in the extended class. Instead, set the value in the constructor of the child classes. Aprotected
property of the parent class is visible in the child classes.EaseNotification
class so that you can access the property from the extended classes. You also should remove the property declaration from the child classes as you won’t be using that property any more.