繁体   English   中英

Zend Framework:从其他文件调用函数

[英]Zend Framework: Calling function from a different file

* * Edit2:未找到Core.php,因为Bootstrap.php需要Core.php包含了require_once'C:\\ wamp \\ www \\ PhpProject1 \\ application \\ Core \\ Core.php'; 解决了我的问题**


我正在使用zendframework,并为我要在控制器和其他地方使用的功能创建了一个新文件夹,文件名为Core / Core.php。 但是,当我尝试在Core.php中测试一个功能时,将不会显示任何内容。 我认为我错误地调用了core.php,但是我不确定错误是什么。

应用程序/控制器/

IndexController.php

class IndexController extends Zend_Controller_Action
{

     public function init()
    {
         $this->tournamentAction();
    }

   public function indexAction(){          

   }

    public function tournamentAction()
  {            
        $bleh = new Core();
        $this->view->ha = $bleh->yo();
        $this->renderScript('tournament/index.phtml');            
    }    
}

应用程序/核心/

Core.php

class Core{

    public function init(){

    }

    public function indexAction(){

    }
    public function yo(){
        $text = 'This is my function Yo';
        return $text;
    }

应用程序/视图/脚本/锦标赛/index.phtml

$this->ha;
echo "hello";

编辑:错误报告是很好的哈! 这是我得到的错误。

 ! ) Fatal error: Class 'Core' not found in C:\wamp\www\PhpProject1\application\controllers\IndexController.php on line 19
Call Stack
#   Time    Memory  Function    Location
1   0.0006  678944  {main}( )   ..\index.php:0
2   0.0275  3090632 Zend_Application->run( )    ..\index.php:26
3   0.0275  3090632 Zend_Application_Bootstrap_Bootstrap->run( )    ..\Application.php:366
4   0.0275  3090728 Zend_Controller_Front->dispatch( )  ..\Bootstrap.php:97
5   0.0446  4801056 Zend_Controller_Dispatcher_Standard->dispatch( )    ..\Front.php:954
6   0.0456  4823424 Zend_Controller_Action->__construct( )  ..\Standard.php:268
7   0.0547  5211576 IndexController->init( )    ..\Action.php:133
8   0.0547  5211576 IndexController->tournamentAction( )    ..\IndexController.php:8

您的问题是自动加载器找不到您的Core类。

通常,我使用所有模型和工具都在其中结束的库文件夹来构造应用程序。

在您的情况下,我将在库文件夹中创建一个名为MyProject的文件夹,在其中您可以放置​​Core.php类,并必须更正其名称,该名称导致:

库/MyProject/Core.php

class MyProject_Core{

public function init(){

}

public function indexAction(){

}
public function yo(){
    $text = 'This is my function Yo';
    return $text;
}

当然,您可以通过以下方式调用课程:

$coreInstance = new MyProject_Core();

将此功能放在您的application / Bootstrap.php中:

protected function _initAutoload()
{
    $autoloader = Zend_Loader_Autoloader::getInstance();
    $autoloader->registerNamespace('MyProject_');
    $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH));
    return $autoloader;
}

然后Zend_Loader应该找到没有任何问题的类。

view

$this->ha; // this doesn't do anything without an echo / print

你还说:

我认为我错误地调用了core.php,但是我不确定错误是什么。

如果是这种情况,则应启用错误报告。

将其添加到引导文件的顶部:

error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM