Don Gorgon
Don Gorgon

Reputation: 2591

problem with including a php file in Zend framework

i have a php file that contains a list of variables (a list of error messages) and nothing else.

in my controller, i included the file using the following line right before the definition of my controller

require_once PATH_TO_FILE;
class mycontrollerController extends Zend_Controller_Action {

all my actions here

}

so if everything goes well , i can access any variable from any action in my controller, however it doesnt work and i get an error message that says that the variable i'm trying to access is not defined.
but if i include the file inside the action, everything works perfect and i can access the variables.

i have no idea why including the file in the top of the controller doesnt seem to work.

Upvotes: 1

Views: 124

Answers (2)

dinopmi
dinopmi

Reputation: 2673

Actually if you paste your variables before the class definition, it won't work either. Only the code inside the class is being taken into account.

In my opinion, it would probably be better to enclose these variables as members of a class (which can be autoloaded, and you won't need to use require_once) and access them from anywhere you need them.

Upvotes: 1

Imad Moqaddem
Imad Moqaddem

Reputation: 1483

Because in Object Oriented Programmation, classes and methods are designed this way: Including the file before the class, doesn't mean your method can access it, like in procedural programmation.

If you want to access the variables, you have to "make them visible" in the scope of your actions.

I suggest you to learn more about OOP and Zend Framework here: http://framework.zend.com/manual/en/

http://php.net/manual/en/language.oop5.php

Upvotes: 2

Related Questions