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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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