php include top.php,PHP include_once

用户评论:

falmeida1988 (email from google.) (2013-03-04 05:47:23)

I would like to point out that (for both include_once and require_once), if you're using these to load code, (i.e. you require/include a file that only returns data), this function will return the data for the first time but only return true for all subsequent times.

Kamil Pawelkiewicz (2012-10-16 15:21:28)

PHP can distinguish if file is a symlink while including.

I've created example.php file containing code below:

Then I've created symlink to example.php in the same directory.

Executing this code in the working directory:

include_once'symlink';?>

prints only 'example.php included' instead of 2 lines.

Frankbrennanfilms at yahoo dot com (2010-09-21 13:43:02)

Need some help please. I keep getting this error:

can not include

Fatal error: Cannot redeclare get_postdata()

Here's the solution I was told to put in, but it's  not working

echo'can not include';

} else {

include('deprecated.php');

}

* @package WordPress* @subpackage Deprecated*/

nospam at freeproxylist dot org (2009-03-30 20:51:58)

I would like to share one very useful tip with include_* statements. For example we have two classes first.class.php and second.class.php both located in the same directory (./classes) and first one uses the second one. So we have:

first.class.php

...?>

also we have file which uses first.class.php:

...?>

if you will try to execute your script you will get error. Reason: the current directory is different and the relative path in first.class.php (./second.class.php) will be incorrect.

Here is two possible solution I have found:

...?>

or

chdir(dirname(__FILE__));

include_once'./second.class.php';

...?>

Hope that tip will be useful for some other software developer

Admin of http://FreeProxyList.org

neil at holcomb dot com (2009-01-07 13:42:36)

Using include_once() in the __autoload() function is redundant. __autoload() is only called when php can't find your class definition. If your file containg your class was already included, the class defenition would already be loaded and __autoload() would not be called. So save a little overhead and only use include() within __autoload()

Neil Holcomb

roach dot scott+spam at googlemail dot com (2008-06-27 17:22:28)

If you include a file that does not exist with include_once, the return result will be false.

If you try to include that same file again with include_once the return value will be true.

Example:

var_dump(include_once'fakefile.ext');// bool(false)var_dump(include_once'fakefile.ext');// bool(true)?>

This is because according to php the file was already included once (even though it does not exist).

emanuele at rogledi dot com (2008-05-18 17:40:08)

For include_once a file in every paths of application we can do simply this

include_once($_SERVER["DOCUMENT_ROOT"] . "mypath/my2ndpath/myfile.php");

php at metagg dot com (2007-08-08 15:29:56)

If you are like me and make heavy use of the __autoload magic function, always set include paths so you can just instantiate your class, and have multiple locations and name schemes for your custom libraries then you might be frustrated by simple parse errors being supressed when using @include_once('lib.php').

The solution I came up with was:

define('IN_PRODUCTION_ENV',FALSE);

function __autoload($class){

$paths = array();

$paths[] = "{$class}_lib.php";

$paths[] = "{$class}_inc.php";

$paths[] = "{$class}.php";

if(IN_PRODUCTION_ENV){

foreach($paths as &$path){

if((@include_once $path) !== false){ return; }//if

}//foreach

}else{

// we are not in a production environment so we want to see all errors...

$include_paths = explode(PATH_SEPARATOR,get_include_path());

foreach($include_paths as $include_path){

// go through each of the different class names...

foreach($paths as $path){

// attach each class name to the include path...

$include_file = $include_path.$path;

if(file_exists($include_file)){

if((include_once $include_file) !== false){ return; }//if

}//if

}//foreach

}//foreach

}//if/else

trigger_error("{$class} was not found",E_USER_ERROR);

}//method

Now, just make sure you define IN_PRODUCTION_ENV to true or false to get either the slower (with all parse errors shown) or the faster (just suppress everything) autoloading. Hope this helps someone else since it was annoying just having blank screens show up when I had a simple parse error. Thanks to flobee at gmail dot com for providing me with the epiphany on why pages were showing up blank...-Metagg

webmaster AT domaene - kempten DOT de (2006-08-10 05:11:46)

Since I like to reuse a lot of code it came handy to me to begin some sort of library that I stored in a subdir

e.g. "lib"

The only thing that bothered me for some time was that although everything worked all IDEs reported during editing

these useless warnings "file not found" when library files included other library files, since my path were given all relative to the corresponding document-root.

Here is a short workaround that makes that gone:

}

include_once ('./lib/other_lib.inc');// ... or any other include[_once] / require[_once]?>

just adjust the path and it will be fine - also for your IDE.

greetings

(2005-08-29 01:52:35)

Dealing with function redefinitions

include_once and require_once are very useful if you have a library of common functions. If you try to override with - that is define - an identically named local function however, PHP will halt noting that it cannot redeclare functions. You can allow for this by bracketing (within the include file):

function myUsefulFunc($arg1, $arg2) {

... }

with

if (!function_exists('myUsefulFunc')) {

function myUsefulFunc($arg1, $arg2) {

... }}

Top level functions (ie. those not defined within other functions or dependent on code running) in the local file are always parsed first, so http://php.net/function_exists within the included/required file is safe - it doesn't matter where the include statements are in the local code.

Csaba Gabor from Vienna

flobee at gmail dot com (2005-05-26 07:55:47)

i already had a discussion with several people about "not shown errors"

error reporting and all others in php.ini set to: "show errors" to find problems:

the answer i finally found:

if you have an "@include..." instead of "include..." or "require..('somthing') in any place in your code

all following errors are not shown too!!!

so, this is actually a bad idea when developing because paser errors will be droped too:

echo'can not include';

}?>

solution:

echo'can not include';

} else {

include('./something');

}?>

Pure-PHP (2005-03-17 14:17:21)

Inlude_once can slower your app, if you include to many files.

You cann use this wrapper class, it is faster than include_once

http://www.pure-php.de/node/19

include_once("includeWrapper.class.php")

includeWrapper::includeOnce("Class1.class.php");

includeWrapper::requireOnce("Class1.class.php");

includeWrapper::includeOnce("Class2.class.php")

bioster at peri dot csclub dot uwaterloo dot ca (2004-10-28 15:06:34)

Something to be wary of: When you use include_once and the data that you include falls out of scope, if you use include_once again later it will not include despite the fact that what you included is no longer available.

So you should be wary of using include_once inside functions.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值