Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>About object inheritance</h2> <p>A class is a template for objects, and defines object properties and methods, while object is instance of a class. When you extend a class, the child class inherits properties and methods from a parent.</p> <p>In your case, there is no inheritance (parent-child relationship), <code>$_header</code> as a separate object is only property of a <code>$_page</code>. To enable a ‘communication’ between those two objects, <code>$_header</code> must have a reference to a <code>$_page</code> object.</p> <hr> <h2>Template class</h2> <p>This is a modified version of the Template class you are using. When you dynamically create properties, <code>__set()</code> and <code>__get()</code> <a href="http://php.net/manual/en/language.oop5.magic.php" rel="nofollow">magic methods</a> should be used. It also uses <code>__toString()</code> method, so that the template objects can be treated as a string. All variables that template file uses, should be assigned to template object. By using a class defined like this, all templates are rendered simultaneously.</p> <pre><code>class tplEngine { private $template = ''; public function __set( $var, $content ) { $this-&gt;$var = $content; } public function __get( $var ) { return ( isset($this-&gt;$var) ? $this-&gt;$var : null ); } public function __construct( $template ) { // is_readable() - tells whether a file exists and is readable if ( !is_readable( $template ) ) throw new IOException( "Could not find or access file: $template" ); $this-&gt;template = $template; } public function __toString() { ob_start(); require ( $this-&gt;template ); $content = ob_get_clean(); return $content; } } // usage: $_page = new tplEngine( TPLFILES_DIR . "/root.php" ); $_header = new tplEngine( TPLFILES_DIR . "/common/header.html" ); $_header-&gt;isLoggedIn = true; $_header-&gt;username = 'some-username'; $_page-&gt;header = $_header; // in root.php echo $this-&gt;header; </code></pre> <hr> <h2>Accessing parent variables</h2> <p><strong>Parent property</strong></p> <p>One way to access variables in 'parent' object is to add parent property to template class through constructor:</p> <pre><code>public function __construct( $template, $parent = null ) { // is_readable() - tells whether a file exists and is readable if ( !is_readable( $template ) ) throw new IOException( "Could not find or access file: $template" ); $this-&gt;template = $template; $this-&gt;_parent = $parent; } </code></pre> <p>Access parent properties in templates like:</p> <pre><code>$this-&gt;_parent-&gt;username; </code></pre> <p><strong>Make parent properties local</strong></p> <p>Another way is to make them local ( neat trick if you don’t want to bother with the <code>$this-&gt;_parent</code> calls ):</p> <pre><code>public function __toString() { ob_start(); if ( $this-&gt;_parent ) { foreach ( get_object_vars( $this-&gt;_parent ) as $key =&gt; $value ) $$key = $value; } require ( $this-&gt;template ); $content = ob_get_clean(); return $content; } </code></pre> <h2>Additional info</h2> <p><a href="http://sourcemaking.com/design_patterns/adapter/php" rel="nofollow">Adapter Design Pattern</a></p> <p><a href="http://www.php.net/manual/en/language.oop5.overloading.php" rel="nofollow">PHP Overloading</a></p> <p><a href="http://php.net/manual/en/language.oop5.magic.php" rel="nofollow">Magic Methods</a></p> <p><a href="http://www.smarty.net/docs/en/language.variable.scopes.tpl" rel="nofollow">Smarty Template Engine - variable scopes</a> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload