obay Posted July 24, 2007 Share Posted July 24, 2007 how do i run a function declared in another php file without using "include" or "require"? for example (below) file1.php contains function connectToDB() which accesses db.conf file to get connection details for my database, then connects to the database. the problem is that if i need to use the function connectToDB() in file2.php, i cannot use "include('../file1.php')" or "require...", because it will try to find folder1.2>folder1.1>db.conf instead of folder1.1>db.conf. (folder1) -file1.php -(folder1.1) --db.conf -(folder1.2) --file2.php so i can't use "include" or "require". what do you suggest? thanks or i can rephrase my question to "how do i run connectToDB() (declared in file1.php) in file2.php as if it was run from file1.php?" help??? Link to comment https://forums.phpfreaks.com/topic/61482-run-function-in-another-php-file-without-using-include-or-require/ Share on other sites More sharing options...
cooldude832 Posted July 24, 2007 Share Posted July 24, 2007 its impossible based on you saying not knowing how to move in levels ../ is root ./ is down one level so down 2 levels ././ but better idea is make a folder called includes or put it in your cgi-bin a file called functions.php that contains all functions related to the work in them and at the top of each page say <?php session_start(); require_once(../includes/functions.php); ?> that way you have sessions going on all pages and all functions waiting to go, don't put any output in functions.php only functions. Link to comment https://forums.phpfreaks.com/topic/61482-run-function-in-another-php-file-without-using-include-or-require/#findComment-306049 Share on other sites More sharing options...
obay Posted July 24, 2007 Author Share Posted July 24, 2007 actually, my file1.php is my functions.php.. the problem arises because file2.php runs connectToDB() in functions.php as if the function is declared in file2.php. so when it accesses a file like db.conf, it can't access it because it's found in another folder. Link to comment https://forums.phpfreaks.com/topic/61482-run-function-in-another-php-file-without-using-include-or-require/#findComment-306055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.