在PHP中:
require
对比include
?require_once
对比include_once
?我读到的关于更好的PHP编码实践的一切都在说不要require_once
因为速度而使用.
为什么是这样?
做同样事情的正确/更好的方法是什么require_once
?如果重要,我使用的是PHP5.
我有以下结构
otsg
> class
> authentication.php
> database.php
> user.php
> include
> config.inc.php
> encryption.php
> include.php
> session.php
> index.php
> registration.php
Run Code Online (Sandbox Code Playgroud)
include.php文件具有以下内容
ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('include_path',ini_get('include_path').':/Applications/MAMP/htdocs/otsg/:');
require_once 'config.inc.php';
require_once '../class/database.php';
require_once '../class/user.php';
require_once 'encryption.php';
require_once 'session.php';
require_once '../class/authentication.php';
Run Code Online (Sandbox Code Playgroud)
在我包含的index.php页面中
require_once 'include/include.php';
Run Code Online (Sandbox Code Playgroud)
当我打开页面index.php时,我收到以下警告和致命错误.我不明白是什么导致了这个错误.当我给出绝对路径时它起作用.但绝对的道路并不是我认为的好主意.
Warning: require_once(../class/database.php) [function.require-once]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/otsg/include/include.php on line 9
Fatal error: require_once() [function.require]: Failed opening required '../class/database.php' (include_path='.:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/htdocs/otsg/include/:') in /Applications/MAMP/htdocs/otsg/include/include.php on line 9
Run Code Online (Sandbox Code Playgroud)
提前致谢
这只是风格差异,还是使用require_once('filename.php')
vs require_once 'filename.php'
有实际的负载/效率差异?
我正在使用require_once这样的
require_once('../mycode.php')
Run Code Online (Sandbox Code Playgroud)
我正在开发一个wordpress插件.我的插件文件夹是yves-slider,我有一个名为yves-slider.php的文件和一个名为admin的文件夹.在admin文件夹中,我有一个名为admin.php的文件.我想在我的admin.php中要求文件yves-slider.php,它位于一个级别的目录中.当我尝试使用时
require_once('../yves-slider.php')
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误
警告:require_once(../ yves-slider.php):无法打开流:C:\ xampp\htdocs\wordpress\wp-content\plugins\yves-slider\yves-slider-admin \中没有此类文件或目录第4行的yves-slider-admin.php
致命错误:require_once():在C:\ xampp\htdocs\wordpress\wp-content \中打开所需的'../yves-slider.php'(include_path ='.; C:\ xampp\php\PEAR')失败第4行的plugins\yves-slider\yves-slider-admin\yves-slider-admin.php
我做错了吗?我正在使用XAMPP 3.1,我猜这是最好的方法.
当我遇到一个奇怪的情况时,我正在用PHP编写一个Web应用程序.为了说明我的问题,请考虑这种结构的Web应用程序:
/
index.php
f1/
f1.php
f2/
f2.php
Run Code Online (Sandbox Code Playgroud)
这些文件的内容:
index.php文件:
<?php require_once("f1/f1.php"); ?>
Run Code Online (Sandbox Code Playgroud)
f1.php:
<?php require_once("../f2/f2.php"); ?>
Run Code Online (Sandbox Code Playgroud)
f2.php:空白
现在当我尝试在浏览器中打开index.php时出现此错误:
Warning: require_once(../f2/f2.php) [function.require-once]:
failed to open stream: No such file or directory in /var/www/reqtest/f1/f1.php on line 2
Fatal error: require_once() [function.require]:
Failed opening required '../f2/f2.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/reqtest/f1/f1.php on line 2
Run Code Online (Sandbox Code Playgroud)
有什么明显的东西我不见了吗?如何在PHP中包含路径?
在我提出这个问题之前,我试图尝试并找出答案.我设置了另一个测试,如下所示:
/
index.php
f1/
f1.php
f2.php
Run Code Online (Sandbox Code Playgroud)
index.php文件:
<?php require_once("f1/f1.php"); ?>
Run Code Online (Sandbox Code Playgroud)
f1.php:
<?php require_once("f2.php"); ?>
Run Code Online (Sandbox Code Playgroud)
f2.php:空白
令我惊讶的是(并且完全混乱),这很好!
那么,路径解析背后的秘诀是什么?
PS我看到了这个问题,但它仍然没有回答我在这里所说的第二个案例.
简单问题:require_once
全球范围是?
例如:
<?PHP
require_once('baz.php');
// do some stuff
foo ($bar);
function foo($bar) {
require_once('baz.php');
// do different stuff
}
?>
Run Code Online (Sandbox Code Playgroud)
调用foo时,是否会重新解析baz.php?或者它是否依赖于主php文件中已经需要的文件(对于相同的包含文件连续两次调用require_once)?
我之前看过这个帖子,但它没有完全回答这个问题:
应该require_once"some file.php"; 出现在文件顶部的任何地方?
谢谢你的帮助!
我正在寻找一种方法来设置范围require_once()
到全局范围,何时require_once()
在函数内部使用.类似下面的代码应该工作:
文件`foo.php':
<?php
$foo = 42;
Run Code Online (Sandbox Code Playgroud)
实际代码:
<?php
function includeFooFile() {
require_once("foo.php"); // scope of "foo.php" will be the function scope
}
$foo = 23;
includeFooFile();
echo($foo."\n"); // will print 23, but I want it to print 42.
Run Code Online (Sandbox Code Playgroud)
有没有办法明确设定范围require_once()
?有一个很好的解决方法吗?
我在一个文件"evalmath.php"中有一个类.
如果我这样要求:require_once('evalmath.php');
该文件的全部内容都会回显到屏幕上.
如果我这样做require_once( 'evalmath.php' );
,它就没有.
咦?
编辑 - EVALMATH.PHP的源代码
<?
/*
================================================================================
EvalMath - PHP Class to safely evaluate math expressions
Copyright (C) 2005 Miles Kaufmann <http://www.twmagic.com/>
================================================================================
NAME
EvalMath - safely evaluate math expressions
SYNOPSIS
<?
include('evalmath.class.php');
$m = new EvalMath;
// basic evaluation:
$result = $m->evaluate('2+2');
// supports: order of operation; parentheses; negation; built-in functions
$result = $m->evaluate('-8(5/2)^2*(1-sqrt(4))-8');
// create your own variables
$m->evaluate('a = e^(ln(pi))');
// or functions
$m->evaluate('f(x,y) = x^2 + y^2 …
Run Code Online (Sandbox Code Playgroud) 似乎如果require_once
在函数内调用,则包含的文件不会扩展全局变量范围.如何require_once
从函数中将文件转换为全局范围?
我正在尝试做的是一些动态模块加载器:
function projects_init()
{
...
foreach ($projects as $p) {
require_once($p['PHPFile']);
$init_func = $p['init'];
if ($init_func)
$init_func();
}
}
Run Code Online (Sandbox Code Playgroud)
如果无法使用require_once
这种方式,最简单的解决方案是什么?(请不要沉重的框架.)
编辑:它也适用于PHP 5.2.
php ×10
require-once ×10
scope ×3
include ×2
directory ×1
fatal-error ×1
include-path ×1
performance ×1
require ×1
warnings ×1