Asked  7 Months ago    Answers:  5   Viewed   19 times

in PHP, how would one return from an included script back to the script where it had been included from?

IE:

1 - main script 2 - application 3 - included

Basically, I want to get back from 3 to 2, return() doesn't work.

Code in 2 - application

$page = "User Manager";
if($permission["13"] !=='1'){
    include("/home/radonsys/public_html/global/error/permerror.php");
    return();
}

 Answers

48

includeme.php:

$x = 5;
return $x;

main.php:

$myX = require 'includeme.php'; 

also gives the same result

This is one of those little-known features of PHP, but it can be kind of nice for setting up really simple config files.

Wednesday, March 31, 2021
 
phirschybar
answered 7 Months ago
17

If you need to determine the base path of a set of scripts, you should not rely on the "current working directory." This can change from executing environment to executing environment.

Instead, base it on a known path.

/includes/class_bootstrap.php knows that it's going to be one directory down from where the base path is going to be, so it can do this:

define('CWD', realpath(dirname(__FILE__) . '/../') );

dirname gets the directory name given in the passed string. If __FILE__ returns C:/wamp/www/vb4/plugins/includes/class_bootstrap.php, then dirname will return C:/wamp/www/vb4/plugins/includes. We then append /../ to it and then call realpath, which turns that relative .. into a real directory: C:/wamp/www/vb4/plugins

Phew.

From that point forward, CWD will operate as you expect. You can require_once CWD . '/includes/init.php' and it will correctly resolve to C:/wamp/www/vb4/plugins/includes/init.php

Also, this may sound stupid but "vb4" may be referring to vBulletin 4, in which case your plugin may already have access to the configuration information that it exposes, including handy things like paths. This may make this entire exercise unnecessary. I intentionally know nothing about vB, otherwise I would point you at their dev docs.

Wednesday, March 31, 2021
 
Exoon
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
27

The includes are evaluated from the location of the running script. When you include another file, you are essentially pulling the contents of that file into the running script at that place.

For files that should evaluate includes relative to the included file's location, you can do this:

/foo/baz.php

include(dirname(__FILE__) . '/bar.inc.php';
include(dirname(__FILE__) . '/../asdf/qwerty.inc.php'

From the documentation:

__FILE__ is The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

dirname Given a string containing the path of a file or directory, this function will return the parent directory's path.

[http://php.net/manual/en/function.dirname.php] [http://php.net/manual/en/language.constants.predefined.php]

Wednesday, August 25, 2021
 
Steve
answered 2 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 :