Justin
Justin

Reputation: 162

how to use your own functions and/or classes in Zend Framework?


i have been creating my set of functions since i started coding and i want to use these functions throughout my project in Zend Framework 1.12
i know i can place in a folder that i would create in the library, however i am looking for a different way to use my functions.
if i placed my functions in:

.../library/myfunctionsfolder/myfunctions.php

i would have to include this file in every controller when i need to use any function from this file.
However i am looking for a way that i can directly call a function from the file in any controller without having to include the file.
For example:

my_function($some_var);

Should i include this file in Zend_Controller or is it a bad idea?
How Can i do that?
Thanks for any help

Upvotes: 0

Views: 703

Answers (1)

bitWorking
bitWorking

Reputation: 12675

Like I said the following points are all valid for ZF1: Autoloader for functions

If you really like to use functions and want to preload them then the best place for doing so is the Bootstrap.php.

So in the Application\Bootstrap.php just create a new method with prefix _init:

protected function _initPreloading()
{
    include LIBRARY_PATH."/myfunctionsfolder/myfunctions.php";
    include LIBRARY_PATH."/myfunctionsfolder/myfunctions2.php";
}
/* For those who does not know how to address the library (in case you cannot use LIBRARY_PATH) You can use this:
include ( APPLICATION_PATH . '/../library/Customfunctions/functions.php'); */

For this to work you need the following 2 lines in your application.ini:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

UPDATE:

To use LIBRARY_PATH I've included the following into index.php:

// Define path to library directory
defined('LIBRARY_PATH') 
    || define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/../library'));

More info on Bootstrapping: Bootstrapping

Another way would be to use a controller plugin for conditional preloading. If you only need certain functions in a controller.

More info on how to write a plugin: Plugins

Upvotes: 2

Related Questions