Jump to content

include/require strange behavior


Manixat

Recommended Posts

Hello freaks,

 

 

I have a file structure like so

 

Framework/App.php

Framework/config.php

Public/Index.php

 

I have put "echo 'test';" in the config.php file for the purpose of testing and here's what I'm experiencing.

 

in my index.php I include App.php like so - include('../Framework/App.php');

Then in the App.php I include config.php like so - include('config.php');

 

And now config.php doesn't throw a warning "no such file" but doesn't echo "test" either.


When I include "config.php" from "index.php" like so - include('../Framework/config.php');

 

Then it echoes "test". 

My question is why this strange behavior, why is it kind of including config from App but not quite doing what it's supposed to ?

Link to comment
https://forums.phpfreaks.com/topic/276132-includerequire-strange-behavior/
Share on other sites

The path for include is relative to the current working directory. This is usually the path of the original executing script (in this case index.php). So include('config.php');, even inside of App.php, is looking in the Public directory not the Framework directory.

 

Why it does not throw an error, could be a couple of things: there is another config.php in the Public directory; or your error reporting value does not cover this warning; or there is a config.php somewhere else in the include_path (from the PHP.ini file).

Alright that worked, but that raises another question. Since the working directory is the original working script directory ( Public ), then how come the classes get included correctly from App.php when the structure is like so:

 

Framework/App.php

Framework/config.php

Framework/classes/

Public/Index.php

 

and the include from my autoload function is like so:

 

include 'classes/' . $class_name . '.php';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.