Чтение this article и работало через пример Decorator. Код возвращает <strong></strong>
вместо ожидаемого <strong><a href="logout.php">Logout</a></strong>
.Отладка отладки PHP
class HtmlLinks {
//some methods which is available to all html links
}
class LogoutLink extends HtmlLinks
{
protected $_html;
public function __construct() {
$this->_html = "<a href=\"logout.php\">Logout</a>";
}
public function setHtml($html) {
$this->_html = $html;
}
public function render() {
echo $this->_html;
}
}
class LogoutLinkStrongDecorator extends HtmlLinks {
protected $_logout_link;
public function __construct($logout_link) {
$this->_logout_link = $logout_link;
$this->setHtml("<strong>" . $this->_html . "</strong>");
}
public function __call($name, $args) {
$this->_logout_link->$name($args[0]);
}
}
$logout_link = new LogoutLink();
$logout_link = new LogoutLinkStrongDecorator($logout_link);
$logout_link->render();
Пробовал отлаживать весь день, но я не сделал никакого прогресса. Любое понимание было бы оценено.
Это часть Я пытался решить. Я пытался сделать это в методе LogoutLinkStrongDecorator __construct (добавив '$ this -> _ html = $ logout_link -> _ html' или' $ this -> _ html = $ this -> _ logout_link -> _ html'). Но все, что я пробовал, приводит к ошибке, например: «Не удается получить доступ к защищенному свойству LogoutLink :: $ _ html' – verymystery
изменить переменную на статическую. Вы также можете удалить расширения ... Я также думаю, что наиболее распространенным способом является использование интерфейса. – Bytemain
Любое ключевое слово видимости работает после того, как я удалил главные символы подчеркивания из объявлений переменных. Изменен '$ this-> setHtml (" ". $ This -> _ html." ");' to '$ this-> setHtml (" ". $ This -> _ logout_link -> _ html." "); ' – verymystery