PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

PHP doing an include statement in a single file but many files will be affected

Does someone know how to make a script that is always included in a php file.
For Example, I have an index.php, I wanted to include script.php by doing include 'script.php'; Again, I have a login.php and I wanted to include script.php by doing include 'script.php';

I think this is very tiring to do and I think this is not a good idea because it is very repetitive. Is there a way where I can include script.php without doing include in each of the file? Like I should do that in a single file then everything would be affected.

Any idea would be greatly appreciated and rewarded. Thanks! :)

Upvotes: 0

Views: 171

Answers (3)

Chris Cherry
Chris Cherry

Reputation: 28574

The only way to literally do what you are asking that I know of is through a config option:

auto_prepend_file

If your PHP code is OO, you could look into cleaner solutions such as autoload. Which I would recommend over the auto_prepend_file. A step down from that would be just use 1 file that you do include from all of your other files maybe named init.php, but then anything else you need included to all of your files you add the include to init.php. Not the cleanest solution, but easy and fast to implement and doesn't involve any config changes.

Upvotes: 2

str
str

Reputation: 45019

You could use auto_prepend_file.

Upvotes: 1

grossvogel
grossvogel

Reputation: 6782

The PHP configuration directive auto_prepend_file does this, but I think it's better to use an autoload function instead. That way, if a class you use is not defined, its source file is automatically located and included. Of course, you have to do a little extra work to structure your filesystem and write the code to locate those files. There are lots of questions on SO that cover autoloading strategies.

Upvotes: 3

Related Questions