我已经开始学习Zend Framework 2,并且如何从类中输出变量的内容时遇到了问题。如何在ZF2中输出变量的内容?
I wrote a simple class like below:
<?php
namespace example\Model;
class example{
protected $name = "sth";
public function show_name(){
echo $this-> name;
}
}
我实例化,它像下面,在Module.php文件:
public function getServiceConfig()
{
return array(
'factories' => array(
// 'example\Model\example' => function($sm){
'example' => function($sm){
// $example_ = new example();
$example_ = new example\Model\example();
return $example_;
},
),
);
}
我写了一个控制器,如下图所示:
namespace example\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
protected $show_example_;
public function indexAction()
{
return new ViewModel(array('example' => $this->show_example()));
// return array();
}
public function show_example()
{
if(!$this->show_example_){
$this->show_example_ = $sm->get('example\Model\exmaple');
}
return $this->show_example_;
}
我也写了一个index.phtml:
<?php
echo $example;
?>
请问请问请你帮助我?
像@Jonathan如下所述,在'index.phtml','$ example'是一个实例你需要调用'show_name'方法而不是试图回显对象本身。 – 2014-11-21 10:43:20