2013-10-16 38 views
1

在例如magento他们有PHP从phtml分离。使用此时,包括在php

我也做同样的事情。 但我想不通一件事了,那就是:

当我有这个PHP脚本:

class aclass extends main{ 
public function redirect(){ 
    require_once($this->frontend_folder . $this->admin_folder . "beheer/edit_account.phtml"); 
} 

public function nav_menu(){ 
    return "<nav>some nav menu things in here</nav>"; 
} 

和 “视图” PHTML脚本:

<!doctype html> 
<html> 
<head> 
</head> 
<body> 
    <div id="wrap"> 
     <?php 
      echo $this->nav_menu(); 
     ?> 
    </div> 
</html> 

“$这个“不起作用,但我怎么才能得到这个工作?

回答

2

您需要实例化视图中的类。

<!doctype html> 
<html> 
<head> 
</head> 
<body> 
    <div id="wrap"> 
     <?php 
      $c = new aclass; // instantiate the class 
      echo $c->nav_menu(); // run the function from the class 
      $c = null; // null the variable, maybe help garbage collection... 
     ?> 
    </div> 
</html> 

这不是使用它的最佳方式,但我希望这个想法很清楚。

编辑:这是一个简单的解决方案,取决于你的archetecture,你可以做很多事情。以最简单的形式,您应该考虑在视图的顶部实例化您的类,然后您可以通过您在整个视图中指定的句柄来引用它。