简体   繁体   中英

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'; 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. But when i try to test a function in my Core.php nothing will show up. I think i am calling the core.php incorrectly but im not sure what the error is.

application/controllers/

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

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

$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.

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 :

library/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 :

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.

In your view you do:

$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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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