简体   繁体   中英

PHP include a class and use it in another included files

In my index.php file I have included my db_connect.php file.

If I include another file (page1.php) in index.php, can I call my DB::query() function in page1.php? I must include db_connect.php to page1.php too?

You may want to include_once('db_connect.php'); or even require_once('db_connect.php'); in any file that you are including that will need to make a database connection. Using these methods the file will not be loaded again if already included with another file. include_once() and require_once() act about the same, but they handle errors differently. You can find a good explanation of that here: Difference between require, include, require_once and include_once?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM