Почему в callback'e ob_start не видит переменную класса?

Проблема такая, хочу перед отправкой контента добавить стили и скрипты в head, логика простая, делаю:
str_replace('</head>', $this->links. '</head>', $buffer);

Но он не видит переменную класса, если вручную вписать что-то типа:
str_replace('</head>', '<script type="text/javascript" src="/js/jquery.js"></script></head>', $buffer);

То все ок. В переменной все ок, если ее задампить, то там стили и скрипты есть, но именно в callback не работает :с

Класс для обработки:
class GeneralClass
{
	private $links;

	function __construct() {
		ob_start(array('self', 'pageAssembly'));
	}

	public function includeComponent($name, $template)
	{
		if ($template === "") $template = "default";

		$defPath = "/core/components/";

		$this->links .= '<link rel="stylesheet" href="' . $defPath . $name . '/templates/' . $template . '/style.css">';
		$this->links .= '<script type="text/javascript" src="' . $defPath . $name . '/templates/' . $template . '/script.js"></script>';

		include_once $defPath . $name . "/templates/" . $template . "/template.php";
	}

	private function pageAssembly($buffer) {
		return str_replace('</head>', $this->links. '</head>', $buffer);
	}
}
  • Вопрос задан
  • 104 просмотра
Решения вопроса 1
@yevgenyyakushov Автор вопроса
Крч, зафиксил. Эта вещь работает ТОЛЬКО со статическими данными, callback в ob_start видит только статику...
Несколько часов на выяснение... В документации нигде не нашел поведение callback в ob_start
Вот так работает:
class GeneralClass
{
	static private $links;

	function __construct() {
		ob_start(array("self", "pageAssembly"));
	}

	public function includeComponent($name, $template)
	{
		if ($template === "") $template = "default";

		$defPath = "/core/components/";

		self::$links .= '<link rel="stylesheet" href="' . $defPath . $name . '/templates/' . $template . '/style.css">';
		self::$links .= '<script type="text/javascript" src="' . $defPath . $name . '/templates/' . $template . '/script.js"></script>';

		include_once $defPath . $name . "/templates/" . $template . "/template.php";
	}

	public function pageAssembly($buffer) {
		return str_replace('</head>', self::$links . '</head>', $buffer);
	}
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы