html - Accessing parent object variables from within the scope of the child object in php -


so have been researching templating engines , how create own simple templating engine. pure learning perspective, read through couple of them 1 here.

using little modified version of class mentioned in above link, thought of testing out encountered problem.

when calling instances of same template class inner html , assigning var/value pair parent instance, i'm unable access main parent's variables within the html (child object).

confusing?

maybe following code help.

so if instanciate template ( template class same 1 mentioned in above link) -

$_page = new tplengine(); $_page->load(tplfiles_dir . "/root.php"); 

and instanciate header.html new instance of tplengine class, , assign variable 1st instance follows -

$_header = new tplenginechild(tplfiles_dir . "/common/header.html"); $_page->set("header", $_header->parse()); 

where...

root.php
---------------

<!doctype html> <html>     <head>         <meta http-equiv="content-type" content="text/html; charset=utf-8">         <meta http-equiv="x-ua-compatible" content="ie=edge" />         <title><?php print $this->title; ?></title>         <meta name="keywords" content="<?php print $this->meta_keywords; ?>" />     <meta name="description" content="<?php print $this->meta_description; ?>" />         <?php foreach($this->styles $stylepath) : ?>         <link rel="stylesheet" type="text/css" href="<?php print $stylepath; ?>" />         <?php endforeach; ?>     </head>     <body>         <div class="body-wrap">             <div class="header">                 <?php print $this->header; ?>             </div>             <div class="content-wrap">                 <?php var_dump($this->mid_content); ?>             </div>             <div class="footer">                 <?php print $this->footer; ?>             </div>         </div>     </body> </html> 

and

header.html
-----------------

<div class="mainheader">     <div class="logo">         webtrack.in'     </div>     <div class="dashboard">          <?php if($this->get(isloggedin) == false) : ?>         <p class="greeting">hello <span class="username"><?php echo this->username; ?></span></p>         <a class="logout">logout</a>         <?php else : ?>         <p class="greeting">hello <span class="username"><?php echo $this->username; ?></span></p>         <p><a onclick="showlogin()">login</a></p>         <form id="loginform" class="login form" action="" method="post">             <input type="text" name="username" value="username" />             <input type="password" name="password" value="password" />         </form>         <?php endif; ?>     </div> </div> <nav>     <ul class="headernav">         <li><a href="/">home</a></li>         <li><a href="/pricing">plans , pricing</a></li>         <li><a href="/aboutus">about us</a></li>     </ul> </nav> 

(in above case $this->get(isloggedin) , this->username variables assigned $_page instance) encounter problem where, in header.html file, unable access variables set under $_page instance of tplengine class.

what best approach solve problem?

everything worked fine when set $_page instance global in header.html. right approach?

about object inheritance

a class template objects, , defines object properties , methods, while object instance of class. when extend class, child class inherits properties , methods parent.

in case, there no inheritance (parent-child relationship), $_header separate object property of $_page. enable ‘communication’ between 2 objects, $_header must have reference $_page object.


template class

this modified version of template class using. when dynamically create properties, __set() , __get() magic methods should used. uses __tostring() method, template objects can treated string. variables template file uses, should assigned template object. using class defined this, templates rendered simultaneously.

class tplengine {     private $template = '';      public function __set( $var, $content )     {         $this->$var = $content;     }      public function __get( $var )     {         return ( isset($this->$var) ? $this->$var : null );     }      public function __construct( $template )     {         // is_readable() - tells whether file exists , readable         if ( !is_readable( $template ) ) throw new ioexception( "could not find or access file: $template" );         $this->template = $template;     }      public function __tostring()      {         ob_start();         require ( $this->template );         $content = ob_get_clean();         return $content;     } }  // usage: $_page = new tplengine( tplfiles_dir . "/root.php" );  $_header = new tplengine( tplfiles_dir . "/common/header.html" ); $_header->isloggedin = true; $_header->username = 'some-username';  $_page->header = $_header;  // in root.php  echo $this->header; 

accessing parent variables

parent property

one way access variables in 'parent' object add parent property template class through constructor:

public function __construct( $template, $parent = null ) {     // is_readable() - tells whether file exists , readable     if ( !is_readable( $template ) ) throw new ioexception( "could not find or access file: $template" );     $this->template = $template;     $this->_parent = $parent; }    

access parent properties in templates like:

$this->_parent->username; 

make parent properties local

another way make them local ( neat trick if don’t want bother $this->_parent calls ):

public function __tostring()  {     ob_start();     if ( $this->_parent ) {         foreach ( get_object_vars( $this->_parent ) $key => $value ) $$key = $value;     }     require ( $this->template );     $content = ob_get_clean();     return $content; } 

additional info

adapter design pattern

php overloading

magic methods

smarty template engine - variable scopes


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -