从php中的子对象范围内访问父对象变量
所以我一直在研究模板引擎以及如何创建自己的简单模板引擎.从一个纯粹的学习角度来看,我读了几个像这样的
here.
使用上面链接中提到的类的一点修改版本,我想到测试它但遇到了问题. 当为内部HTML调用相同模板类的实例,然后将其作为var / value对分配给父实例时,我无法访问HTML(子对象)中的主要父变量. 混乱? 也许以下代码会有所帮助. 因此,如果我将模板设置为如此(模板类与上面链接中提到的模板类相同) – $_page = new tplEngine(); $_page->load(TPLFILES_DIR . "/root.php"); 然后将header.html实例化为tplEngine类的新实例,并分配 $_header = new tplEngineChild(TPLFILES_DIR . "/common/header.html"); $_page->set("header",$_header->parse()); 哪里… 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 as $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> 和 了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 and Pricing</a></li> <li><a href="/aboutUs">About Us</a></li> </ul> </nav> (在上面的例子中,$this-> get(isLoggedIn)和this-> username是分配给$_page实例的变量) 解决这个问题的最佳方法是什么? 当我在header.html中将$_page实例设置为全局时,一切正常.但这是一种正确的方法吗? 解决方法
关于对象继承
类是对象的模板,定义对象属性和方法,而对象是类的实例.扩展类时,子类从父级继承属性和方法. 在您的情况下,没有继承(父子关系),$_header作为单独的对象只是$_page的属性.要在这两个对象之间启用“通信”,$_header必须具有对$_page对象的引用. 模板类 这是您正在使用的Template类的修改版本.动态创建属性时,应使用__set()和__get()magic methods.它还使用__toString()方法,以便可以将模板对象视为字符串.模板文件使用的所有变量都应分配给模板对象.通过使用这样定义的类,可以同时呈现所有模板. 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 a file exists and is 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; 访问父变量 父母财产 在’parent’对象中访问变量的一种方法是通过构造函数将父属性添加到模板类: 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->template = $template; $this->_parent = $parent; } 访问以下模板中的父属性: $this->_parent->username; 将父属性设为本地 另一种方法是让它们成为本地的(如果你不想打扰$this-> _parent调用那么巧妙的技巧): public function __toString() { ob_start(); if ( $this->_parent ) { foreach ( get_object_vars( $this->_parent ) as $key => $value ) $$key = $value; } require ( $this->template ); $content = ob_get_clean(); return $content; } 附加信息 Adapter Design Pattern PHP Overloading Magic Methods Smarty Template Engine – variable scopes (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |