简体   繁体   English

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

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

* *Edit2: Core.php was not found because Bootstrap.php needed Core.php included require_once 'C:\\wamp\\www\\PhpProject1\\application\\Core\\Core.php'; * * Edit2:未找到Core.php,因为Bootstrap.php需要Core.php包含了require_once'C:\\ wamp \\ www \\ PhpProject1 \\ application \\ Core \\ Core.php'; Solved my Problem ** 解决了我的问题**


I am using zendframework and i created a new folder with file Core/Core.php for my functions that i will use in my controllers and elsewhere. 我正在使用zendframework,并为我要在控制器和其他地方使用的功能创建了一个新文件夹,文件名为Core / Core.php。 But when i try to test a function in my Core.php nothing will show up. 但是,当我尝试在Core.php中测试一个功能时,将不会显示任何内容。 I think i am calling the core.php incorrectly but im not sure what the error is. 我认为我错误地调用了core.php,但是我不确定错误是什么。

application/controllers/ 应用程序/控制器/

IndexController.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');            
    }    
}

application/Core/ 应用程序/核心/

Core.php Core.php

class Core{

    public function init(){

    }

    public function indexAction(){

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

application/views/scripts/tournament/ index.phtml 应用程序/视图/脚本/锦标赛/index.phtml

$this->ha;
echo "hello";

Edit: Error reporting is nice ha! 编辑:错误报告是很好的哈! This is the error i get. 这是我得到的错误。

 ! ) 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

You problem is that the autoloader can't find your Core class. 您的问题是自动加载器找不到您的Core类。

Usually I structure my application by using a library folder where all my models and tools end. 通常,我使用所有模型和工具都在其中结束的库文件夹来构造应用程序。

In your case, I would create in the library folder a folder called MyProject, inside it you can put your Core.php class, put you'll have to correct it's name which leads to : 在您的情况下,我将在库文件夹中创建一个名为MyProject的文件夹,在其中您可以放置​​Core.php类,并必须更正其名称,该名称导致:

library/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;
}

Of course you'd to call your class this way: 当然,您可以通过以下方式调用课程:

$coreInstance = new MyProject_Core();

Put this function in your application/Bootstrap.php : 将此功能放在您的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;
}

Then the Zend_Loader should find your class whithout any problem. 然后Zend_Loader应该找到没有任何问题的类。

In your view you do: view

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

Also you said: 你还说:

I think i am calling the core.php incorrectly but im not sure what the error is. 我认为我错误地调用了core.php,但是我不确定错误是什么。

If that's the case you should enable error reporting. 如果是这种情况,则应启用错误报告。

Add this at the top of your bootstrap file: 将其添加到引导文件的顶部:

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