Jump to content

run function in another php file without using include or require


obay

Recommended Posts

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???

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. 

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.

Archived

This topic is now archived and is closed to further replies.

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