Properties and dependency-injection

PoP operates in a strictly top-down manner: parent modules know what inner modules they contain, down the line to the last module, and can change those modules’ properties; however, inner modules do not know who is including them, and do not need to care either.

When initializing properties, parent modules take precedence over inner modules: when a module initializes itself, if any one property has already been set by a parent module, then that value will be kept. This way, PoP allows for dependency-injection, where each module can set its default attributes, yet allow for properties to be injected externally in advance (possibly coming from a configuration file).

class GD_EM_Template_Processor_ScrollMapBlocksBase extends GD_Template_Processor_BlocksBase {

	function init_atts($template_id, &$atts) {

		$this->append_att($template_id, $atts, 'class', 'map');
		
		$this->add_att($template_id, $atts, 'direction', $this->get_map_direction($template_id, $atts));
		$direction = $this->get_att($template_id, $atts, 'direction');

		$mapblock_inner_template = $this->get_block_inner_template($template_id);
		$this->add_att($mapblock_inner_template, $atts, 'direction', $direction);
		
		return parent::init_atts($template_id, $atts);
	}

	// ...
}

(Source) A module initializing its own attributes, and also its inner modules’ attributes


Welcome to the PoP framework!
Break the information monopoly

the PoP framework is open source software which aims to decentralize the content flow and break the information monopoly from large internet corporations. Read more.


Sign up to our newsletter: