Renderer class - why these two variables are different

Discuss general development subjects that are not specific to a particular version like the versioning control system we use or other infrastructure.
Post Reply
User avatar
mikovchain
Registered User
Posts: 45
Joined: Mon Oct 16, 2006 5:16 am

Renderer class - why these two variables are different

Post by mikovchain » Thu Dec 06, 2018 4:57 am

I am reading the code of content rendering in phpBB3.2, but I found something that very confusing to me. I am not sure how it works, the code is located at phpbb/textformatter/s9e/renderer.php
there are two lines in this function

Code: Select all

	public function render($xml)
	{
		if (isset($this->quote_helper))
		{
			$xml = $this->quote_helper->inject_metadata($xml);
		}

		$renderer = $this;

		/**
		* Modify a parsed text before it is rendered
		*
		* @event core.text_formatter_s9e_render_before
		* @var \phpbb\textformatter\s9e\renderer renderer This renderer service
		* @var string xml The parsed text, in its XML form
		* @since 3.2.0-a1
		*/
		$vars = array('renderer', 'xml');
		extract($this->dispatcher->trigger_event('core.text_formatter_s9e_render_before', compact($vars)));

		$html = $this->renderer->render($xml);
		if (isset($this->censor) && $this->viewcensors)
		{
			$html = $this->censor->censorHtml($html, true);
		}

		/**
		* Modify a rendered text
		*
		* @event core.text_formatter_s9e_render_after
		* @var string html The rendered text's HTML
		* @var \phpbb\textformatter\s9e\renderer renderer This renderer service
		* @since 3.2.0-a1
		*/
		$vars = array('html', 'renderer');
		extract($this->dispatcher->trigger_event('core.text_formatter_s9e_render_after', compact($vars)));

		return $html;
	}
one is

Code: Select all

		$renderer = $this;
another is

Code: Select all

		$html = $this->renderer->render($xml);
To my understanding, in this function the variable $renderer equals to $this->renderer, and assign $this to $renderer will make a recursive reference, while at the second line, this will invoke this function itself again.

But the var_dump output shows these $renderer and $this->renderer are different objects.
I hadn't programmed PHP for like 6 years so my knowledges are quite outdated, could someone help me to understand this code?

Many thanks!
PHPBB CHINA For Chinese Users

Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 366
Joined: Thu Sep 16, 2004 9:02 am
Contact:

Re: Renderer class - why these two variables are different

Post by Paul » Thu Dec 06, 2018 8:14 am

$this->renderer is a property on the class, while $renderer is a local variable in that method.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 16
Joined: Thu May 24, 2012 1:54 pm

Re: Renderer class - why these two variables are different

Post by kasimi » Thu Dec 06, 2018 8:26 am

mikovchain wrote:
Thu Dec 06, 2018 4:57 am
the variable $renderer equals to $this->renderer
Not quite:

$renderer !== $this->renderer

because

$renderer === $this

therefore

$renderer->renderer === $this->renderer

GanstaZ
Registered User
Posts: 15
Joined: Wed Apr 11, 2018 4:58 pm

Re: Renderer class - why these two variables are different

Post by GanstaZ » Thu Dec 06, 2018 3:50 pm

Just one thing to add about those 2 quotes out of 3, is that thanks to those lines of code, one can connect to events and modify those mixed vars if needed.

User avatar
mikovchain
Registered User
Posts: 45
Joined: Mon Oct 16, 2006 5:16 am

Re: Renderer class - why these two variables are different

Post by mikovchain » Thu Dec 06, 2018 7:11 pm

kasimi wrote:
Thu Dec 06, 2018 8:26 am
Not quite:

$renderer !== $this->renderer

because

$renderer === $this

therefore

$renderer->renderer === $this->renderer
Thanks Kasimi, now it is clear... then this line $renderer === $this didn't do anything here?
PHPBB CHINA For Chinese Users

User avatar
posey
Registered User
Posts: 29
Joined: Fri Dec 18, 2015 9:41 pm

Re: Renderer class - why these two variables are different

Post by posey » Thu Dec 06, 2018 7:36 pm

Yes it does, it made that specific renderer available in the events.
Better known as mrgoldy.

Post Reply