4

How do I call a function which is in a different file without including or requiring that file?

7
  • as i have to include a basic function file always in every script where connection.php is being called... Commented May 7, 2011 at 3:50
  • Why you don't want to use include? If the file has so many other things, refactor it so that the function is isolated in some file and include it. Or look up web services that expose functions (I'm not sure why you want to do this,so explain more) Commented May 7, 2011 at 3:51
  • but the problem is to make file path of basicfunction.php as it is always changing with reference to open directory Commented May 7, 2011 at 3:51
  • Ok , copy paste your code and we'll see where you're going wrong. Commented May 7, 2011 at 3:52
  • $path_determine = explode ("/", $_SERVER['PHP_SELF']); $totSlash = 4; $RelativePath = str_repeat("../", count($path_determine)-$totSlash+1); Commented May 7, 2011 at 3:56

3 Answers 3

5

You can't. If you don't include or require it, PHP has no idea what's there. You can use .htaccess or php.ini to automatically include a file, which is identical to including it with require or include.

For example in .htaccess

php_value auto_prepend_file "/full/path/to/a/prepend-file.php"

In php.ini

  auto_prepend_file = "/full/path/to/a/prepend-file.php"
Sign up to request clarification or add additional context in comments.

2 Comments

can u explane?? please describe how to edit php.ini of server machine??
Depends on the OS and how PHP was installed, and the web server. For example, on my Ubuntu 11 workstation, it's /etc/php5/php.ini after installing with aptitude. Find it, edit it.
3

Read it into a string and eval it.

$code = file_get_contents("somefile.php");
eval($code);

1 Comment

+1 well, with some restrictions in PHP file, this will work fine ;)
0

Copy and paste it. But mind the license.

1 Comment

You violate one of the first software engineering principles that way. Reusuability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.