Asked  9 Months ago    Answers:  5   Viewed   105 times

Here is the full context of the situation:

I recently got a new Mac, I'm a php developer so I downloaded MAMP and started developing.

First I noticed that my includes were not being included, but I changed that by configuring my php.ini.

However now, when I try to include a file with a function it does not recognize the function.

For example I have a file named functions.php:

<?php
function doit(){
    echo "did it";
}
?>

and a file that includes it called index.php

<?php include("functions.php"); doit();?>

and I get this error message

Fatal error: Call to undefined function doit() in index.php on line 4

 Answers

98

So if anyone ever stumbles on this forum because they are having the same issue let me explain what and why it went wrong.

If you include a function not in your directory(e.g c:// or file://) but instead include using http. The include can only return what was echoed in the file, but something like a variable or function will not be shown. So always include functions and variables through a directory

Wednesday, March 31, 2021
 
BetaRide
answered 9 Months ago
52

Here are three possible solutions. The second are really just work-arounds that use absolute paths in a clever way.

1: chdir into the correct directory

<?php

// check if the 'StoredProcedure' folder exists in the current directory
// while it doesn't exist in the current directory, move current 
// directory up one level.
//
// This while loop will keep moving up the directory tree until the
// current directory contains the 'StoredProcedure' folder.
//
while (! file_exists('StoredProcedure') )
    chdir('..');

include_once "StoredProcedure/connect.php";
// ...
?>

Note that this will only work if your StoredProcedure folder is in the topmost directory of any files that might need to include the files it contains.

2: Use absolute paths

Now before you say this is not portable, it actually depends on how you implement it. Here's an example that works with Apache:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/StoredProcedure/connect.php";
// ...
?>

Alternatively, again with apache, put the following in your .htaccess in the root directory:

php_value auto_prepend_file /path/to/example.php

Then in example.php:

<?php

define('MY_DOC_ROOT', '/path/to/docroot');

?>

And finally in your files:

<?php
include_once MY_DOC_ROOT . "/StoredProcedure/connect.php";
// ...
?>

3: Set PHP's include_path

See the manual entry for the include_path directive. If you don't have access to php.ini, then this can be set in .htaccess, providing you are using Apache and PHP is not installed as CGI, like so:

php_value include_path '/path/to/my/includes/folder:/path/to/another/includes/folder'
Wednesday, March 31, 2021
 
oroshnivskyy
answered 9 Months ago
37

http://apigen.juzna.cz/doc/sebastianbergmann/phpunit/function-assertInstanceOf.html

I think you are using this function wrong. Try:

$this->assertInstanceOf('User', $user);
Wednesday, March 31, 2021
 
coolguy
answered 9 Months ago
100

In Form.php use

include_once dirname(__FILE__) . '/../GridView/GridView.php';

This creates an absolute path, but relative to the file, where its called from.

Wednesday, August 4, 2021
 
Rumata
answered 4 Months ago
81

Just so you're aware, if you're going to have the php pages you're requesting also request other pages themselves, it may be beneficial to use require_once instead of include. That will make it so none of the pages that are included repeat themselves and you don't have to worry about accidentally including something more than once.

That being said... when you request a page in the root directory, it will request header.php in the root directory which will in turn request functions.php in the root directory. However, if you request from the subdirectory, ../header.php will reference header.php in the root directory, but that whole file will get included, and then its the php page in the subdirectory that ends up trying to include /functions.php. It would need to request ../functions.php, but that would cause everything in the root directory to stop working.

I'd suggest setting a variable in header.php along the lines of $root = $_SERVER['DOCUMENT_ROOT']; Then, all includes in header.php should be like include($root."/functions.php");

$_SERVER['DOCUMENT_ROOT'] will get you objective url to the root, which will enable you to make sure you're referencing the correct place no matter where you're requesting header.php from.

Thursday, August 5, 2021
 
Damjan Krowalka
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share