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

Link to comment
Share on other sites

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';
Edited by Manixat
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.