簡體   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