How to connect a file?

Hello!

Had a problem, connect the file in the project require_once("project/core/Index.php") all is well, everything works.

Also connect the file to the class for a ajax'says that the file is not found and finds only if you specify an absolute path
require_once("/home/name/public_html/name/project/core/Index.php")


But all the pain is that if you write an absolute path, the base file connection is not working, and files are connected as needed...

Help prlctl
September 19th 19 at 13:10
5 answers
September 19th 19 at 13:12
Solution
Try
require_once __DIR__ // relative path to the current script

hence, in any directory to get
Thanks for the reply, all is Yes, but no.
Files are connected in the main class, something like that
public function __get($name)
{ require_once('games/core/' . $this->classes[$name] . '.php'); }
- barton commented on September 19th 19 at 13:15
: What prevents change "games/core/" to dirname(__FILE__), and to prescribe the rest of the way to you directories? Just do var_dump(dirname(__FILE__)); to see where the current file, and append other path. - alexandrine.Gutmann40 commented on September 19th 19 at 13:18
: __DIR__ instead of dirname(__FILE__)
if you do not use a furry version of PHP. - sean_Hilll commented on September 19th 19 at 13:21
September 19th 19 at 13:14
Solution
You can read the directory of the current file dirname(__FILE_) and from it to dance in each place where you need includ
Just above ouie answered, but I will repeat.

Thanks for the reply, all is Yes, but no.
Files are connected in the main class, something like that
public function __get($name)
{ require_once('games/core/' . $this->classes[$name] . '.php'); }
- barton commented on September 19th 19 at 13:17
: you have a file, which acts as the entry point for all addresses, including ajax? For example some ini.php? Make it define('ROOT_DIR', dirname(__FILE__)); and use this constant everywhere in include you know relative paths for all files from the entry point. - alexandrine.Gutmann40 commented on September 19th 19 at 13:20
on some hosts somehow poorly perceived require_once. Try include() - sean_Hilll commented on September 19th 19 at 13:23
discover the __DIR__ - barton commented on September 19th 19 at 13:26
September 19th 19 at 13:16
Solution
about autoload ever heard?
September 19th 19 at 13:18
Solution
Thank you all!
All the options studied, but for some magical reason, the proposed options were processed with an error.
And a working version was like this
require_once($_SERVER['DOCUMENT_ROOT'] . 'project/core/' . $class . '.php');
September 19th 19 at 13:20
What is "the underlying connection files" and how exactly it "doesn't work"?

Find more questions by tags PHP