1

I have some general functions in myBundle/Controller/functions.php and I want to include this php file in another php.

In myBundle/Service/myService.php I do:

require_once ('../Controller/functions.php');

but when running i keep getting the error message 'ContextErrorException: Warning: require_once(functions.php): failed to open stream: No such file or directory in ..../myBundle/Services/myService.php line ..'

Looks so stupid, it must be something simple i Guess. Thanks for your suggestions.

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
PBR
  • 306
  • 4
  • 15
  • You should be taking advantage of namespaces and use keywords instead of include or require. – Ghassan Idriss Mar 11 '14 at 02:52
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 10 '16 at 10:45

2 Answers2

4

I think you did not use namspace in your class. If you use namespace , then it is easy to include those file more easy and comfortable.

If your name space is :

Acme\Bundle\BlogBundle

Then include it in your desired class using:

use Acme\Bundle\BlogBundle;

Also you need to meet the rules defined in PSR-0 (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md) to get it work.

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
hizbul25
  • 3,829
  • 4
  • 26
  • 39
  • The functions I wanna include are no classes... require_once() is still valid php command. not? – PBR Mar 11 '14 at 10:32
  • require_once(), include() this type row php function is not available in Symfony2. Hence your method is not inside class, so you should create a class and put this function inside class. Remember, always use namspace. here you get your comments answer : http://stackoverflow.com/questions/14410946/require-once-at-symfony – hizbul25 Mar 11 '14 at 10:52
0

Searching the Symfony sources I found a way to include the php file. From within a php-source in myBundle/Services use

require_once __DIR__."/../Controller/functions.php";

instead of

require_once "../Controller/functions.php";
PBR
  • 306
  • 4
  • 15