php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63026 require_once error
Submitted: 2012-09-07 05:28 UTC Modified: 2012-09-13 06:15 UTC
From: ian dot xspace at yahoo dot cn Assigned:
Status: Not a bug Package: *Compile Issues
PHP Version: 5.3.16 OS: windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
20 + 29 = ?
Subscribe to this entry?

 
 [2012-09-07 05:28 UTC] ian dot xspace at yahoo dot cn
Description:
------------
<?php 
//OS: win7  php:5.3.16 mysql:5.5
//such as  [Some code snippets]
//Assume  WEB_ROOT = 'E:/www';
define('WEB_ROOT', strtr(dirname(__FILE__), '\\', '/'));
//define the web system directory
define('TEMP', WEB_ROOT.DS.'temp');
define('LIBS', WEB_ROOT.DS.'libs');
define('VIEWS', WEB_ROOT.DS.'views');
define('LOGS', VIEWS.DS.'logs');
define('DYN', VIEWS.DS.'dynamic');
define('HLP', WEB_ROOT.DS.'helpers');
define('PLNS', WEB_ROOT.DS.'plugins');
define('CONFS', WEB_ROOT.DS.'configs');
define('MODELS', WEB_ROOT.DS.'models');
define('CTRLS', WEB_ROOT.DS.'controls');
//define  associate
/*    Error writing       */
class SYS
{
	private function sysModule()
	{
		return array(
				'M'=>'MODELS', 'V'=>'VIEWS',
				'C'=>'CTRLS', 'L'=>'LIBS',
				'P'=>'PLNS', 'H'=>'HLP'
		);
	}
	
	public static function getSys($fun)
	{
		return self::$fun();
	}	
}	

//common.php load some calss file
//Assume  $type = 'M.className';
function loadSomeClass($type)
{
	$csName = substr($cs, 2);
	$type = strtoupper(strtok($cs, '.'));
	$moArr = SYS::getSys('sysModule'); //find module
	require_once($moArr[$type].DS."{$csName}.php");
}

//bugs: In accordance with the above wording some successful and some can not successfully  ?????

/*
 	private function sysModule()
	{
		return array(
				'M'=>MODELS, 'V'=>VIEWS,
				'C'=>CTRLS, 'L'=>LIBS,
				'P'=>PLNS, 'H'=>HLP
		);
	} 
 */

?>

Test script:
---------------
<?php 
//OS: win7  php:5.3.16 mysql:5.5
//such as  [Some code snippets]
//Assume  WEB_ROOT = 'E:/www';
define('WEB_ROOT', strtr(dirname(__FILE__), '\\', '/'));
//define the web system directory
define('TEMP', WEB_ROOT.DS.'temp');
define('LIBS', WEB_ROOT.DS.'libs');
define('VIEWS', WEB_ROOT.DS.'views');
define('LOGS', VIEWS.DS.'logs');
define('DYN', VIEWS.DS.'dynamic');
define('HLP', WEB_ROOT.DS.'helpers');
define('PLNS', WEB_ROOT.DS.'plugins');
define('CONFS', WEB_ROOT.DS.'configs');
define('MODELS', WEB_ROOT.DS.'models');
define('CTRLS', WEB_ROOT.DS.'controls');
//define  associate
/*    Error writing       */
class SYS
{
	private function sysModule()
	{
		return array(
				'M'=>'MODELS', 'V'=>'VIEWS',
				'C'=>'CTRLS', 'L'=>'LIBS',
				'P'=>'PLNS', 'H'=>'HLP'
		);
	}
	
	public static function getSys($fun)
	{
		return self::$fun();
	}	
}	

//common.php load some calss file
//Assume  $type = 'M.className';
function loadSomeClass($type)
{
	$csName = substr($cs, 2);
	$type = strtoupper(strtok($cs, '.'));
	$moArr = SYS::getSys('sysModule'); //find module
	require_once($moArr[$type].DS."{$csName}.php");
}

//bugs: In accordance with the above wording some successful and some can not successfully  ?????

/*
 	private function sysModule()
	{
		return array(
				'M'=>MODELS, 'V'=>VIEWS,
				'C'=>CTRLS, 'L'=>LIBS,
				'P'=>PLNS, 'H'=>HLP
		);
	} 
 */

?>

Expected result:
----------------
	private function sysModule()
	{
		return array(
				'M'=>'MODELS', 'V'=>'VIEWS',
				'C'=>'CTRLS', 'L'=>'LIBS',
				'P'=>'PLNS', 'H'=>'HLP'
		);
	}

error writing!
why require_once  some successful and some error


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-07 17:19 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2012-09-07 17:19 UTC] laruence@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2012-09-10 07:44 UTC] ian dot xspace at yahoo dot cn
private function sysModule()
	{
                //error writing 错误写法
		return array(
				'M'=>MODELS, 'V'=>VIEWS,
				'C'=>CTRLS, 'L'=>LIBS,
				'P'=>PLNS, 'H'=>HLP
		);
                //right wring 正确写法
		return array(
				'M'=>MODELS, 'V'=>VIEWS,
				'C'=>CTRLS, 'L'=>LIBS,
				'P'=>PLNS, 'H'=>HLP
		);

	}

//common.php load some calss file
//Assume  $type = 'M.className';
function loadSomeClass($type)
{
	$csName = substr($cs, 2);
	$type = strtoupper(strtok($cs, '.'));
	$moArr = SYS::getSys('sysModule'); //find module
	require_once($moArr[$type].DS."{$csName}.php"); 
        //Error wording actually some successful and some not successful
        //在错误写法下居然有的能成功有的不能成功
}
 [2012-09-10 07:53 UTC] laruence@php.net
你用中文描述吧, 我实在看不懂你说什么错误.... 
另外, 请提供一个可以正常运行的可重试脚本. 
(you can re-describe this problem in chinese, previous one is hard to read).

thanks
 [2012-09-12 01:48 UTC] ian dot xspace at yahoo dot cn
早说啊,我还以为都是外国人呢。
//定义网站跟目录 init.php
define('WEB_ROOT', strtr(dirname(__FILE__), '\\', '/'));
//定义网站的其它目录
define('TEMP', WEB_ROOT.DS.'temp');
define('LIBS', WEB_ROOT.DS.'libs');
define('VIEWS', WEB_ROOT.DS.'views');
//下面是我写的一个类 sys.php
class SYS
{
        //这个是每个目录对应的简写,但是这是错误写法,可在后期的程序调用中居然有的
        //可以成功有的失败了  $moArr = SYS::getSys('sysModule');
        //require_once($moArr[$type].DS."{$csName}.php"); 这样可以直接
        //引入所需要的文件,避免使用switch浪费时间
        /*
// 		switch($type)  以上将会获取更快执行速度
// 		{
// 			case 'M':
// 				require_once(MODELS.DS."$csName.php");
// 			break;
// 			case 'V':
// 				require_once(VIEWS.DS."$csName.php");
// 			break;
// 			case 'C':
// 				require_once(CTRLS.DS."$csName.php");
// 			break;
// 			case 'L':
// 				require_once(LIBS.DS."$csName.php");
// 			break;
// 			case 'P':
// 				require_once(PLNS.DS."$csName.php");
// 			break;
// 			case 'H':
// 				require_once(HLP.DS."$csName.php");
// 			break;
// 			default:
// 				JS::willJS('alertMsg', '调用失败!');
// 		}
       */
       //这里是系统目录对应的单词简写,但是这是错误写法
       //正确写法应该是  'M'=>MODELS 后面无需加单引号(虽然是错误写法但是使用的时候
       //有的可以正常使用有的不能正常使用)
	private function sysModule()
	{
		return array(
				'M'=>'MODELS', 'V'=>'VIEWS',
				'C'=>'CTRLS', 'L'=>'LIBS',
				'P'=>'PLNS', 'H'=>'HLP'
		);
	}
	
	public static function getSys($fun)
	{
		return self::$fun();
	}
}

//common.php 其它程序共享类
class Common
{
   //类中的一个函数用于调用所需要的其他类
   public function loadSomeClass($type)
   {
	$csName = substr($cs, 2);
	$type = strtoupper(strtok($cs, '.'));
	$moArr = SYS::getSys('sysModule'); //find module
	require_once($moArr[$type].DS."{$csName}.php");
   }
}

//index.php 比如在index.php页面里调用其他类文件
class Index
{
   public function __construct()
   {
     require_once 'common.php';
     $com = new Common();
     $com->loadSomeClass('M.db_model');//引入这个类文件或者mvc里的任何一个文件
    //问题就出在这里  'M'=>'MODELS', 这种写法是有错的,但是调用loadSomeClass
    //时候居然有的类可以正确加载而有的类不能正确加载
   }
}
new Index();

//要是还看不明名,你告诉我从哪里上传文件啊,或者我的QQ是995668790,以前我在你的博客里和你
聊过ian,就说有bug的
 [2012-09-12 01:56 UTC] ian dot xspace at yahoo dot cn
//init.php 定义网站目录
/***************************网站目录定义***************************/
define('THEME', 'default');
//define('TEMP', WEB_ROOT.DS.'temp');
//define('LIBS', WEB_ROOT.DS.'libs');
define('VIEWS', WEB_ROOT.DS.'views');
define('LOGS', VIEWS.DS.'logs');
define('DYN', VIEWS.DS.'dynamic');
define('HLP', WEB_ROOT.DS.'helpers');
//define('PLNS', WEB_ROOT.DS.'plugins');
define('CONFS', WEB_ROOT.DS.'configs');
define('MODELS', WEB_ROOT.DS.'models');
define('CTRLS', WEB_ROOT.DS.'controls');
//SYS.php  sysModule函数里的对应关系为错误写法,正确写法应该是'M'=>MODELS后者无需单引号
class SYS
{
	//sysModule系统目录映射
	private function sysModule()
	{
		return array(
		'M'=>MODELS, 'V'=>VIEWS,
		'C'=>CTRLS, 'L'=>LIBS,
		'P'=>PLNS, 'H'=>HLP
		);
	}
	//getSys获取相应配置
	public static function getSys($fun)
	{
		return self::$fun();
	}
}
//common.php 其中一个函数,加载所需要的类
class Common
{
	//loadCS
	protected function loadCS($cs)
	{
		$csName = substr($cs, 2);
		$type = strtoupper(strtok($cs, '.'));
		$moArr = SYS::getSys('sysModule'); //直接定位		
		require_once($moArr[$type].DS."{$csName}.php");
// 		switch($type)  以上将会获取更快执行速度
// 		{
// 			case 'M':
// 				require_once(MODELS.DS."$csName.php");
// 			break;
// 			case 'V':
// 				require_once(VIEWS.DS."$csName.php");
// 			break;
// 			case 'C':
// 				require_once(CTRLS.DS."$csName.php");
// 			break;
// 			case 'L':
// 				require_once(LIBS.DS."$csName.php");
// 			break;
// 			case 'P':
// 				require_once(PLNS.DS."$csName.php");
// 			break;
// 			case 'H':
// 				require_once(HLP.DS."$csName.php");
// 			break;
// 			default:
// 				JS::willJS('alertMsg', '调用失败!');
// 		}
		$CS = ucfirst($csName);
		return new $CS();
	}
}

//index.php 首页调用其它类
//Index
class Index extends IAN_C
{
 	//__construct
	public function __construct()
	{
           $obj = $this->loadCS('M.share_model');//调用其它类
           //你可以多调用几个类,在上面错误写法情况下,有的类可以调用成功,有的失败
           //sysModule 里的对应关系,应该这么写'C'=>CTRLS,后者无需引号
	}

}
new Index

//大哥你要是再看不明白,我也没辙了
 [2012-09-12 02:03 UTC] laruence@php.net
那些类名可以正确加载 ,那些类名不可以?  你举个例子?
 [2012-09-12 10:15 UTC] reeze@php.net
看你这情况,有可能是你的大小写问题, 你是直接require_once的,如果大小写错了是有问题的。
为了好调试,你可以在require_once 前加一个if(!file_exists($file) {die($file not found)}
检查一下。 你确认一下文件的确存在吧。

同时,是在不行,把你的代码完整打包传到某个地方吧。。
 [2012-09-12 10:25 UTC] laruence@php.net
你从哪里看出来和大小写有关系的?
 [2012-09-12 10:30 UTC] reeze@php.net
“$obj = $this->loadCS('M.share_model');//调用其它类
           //你可以多调用几个类,在上面错误写法情况下,有的类可以调用成功,有的失败
           //sysModule 里的对应关系,应该这么写'C'=>CTRLS,后者无需引号
”
require_once的路径是直接拼的,而且不是autoload,
他这里没有其他的例子,如果部分类名大小写错了,就会出现部分类找不到的情况。

在require前增加个检查就能确认或者排除这个问题了
 [2012-09-13 05:46 UTC] ian dot xspace at yahoo dot cn
return array(
				'M'=>'MODELS', 'V'=>'VIEWS',
				'C'=>'CTRLS', 'L'=>'LIBS',
				'P'=>'PLNS', 'H'=>'HLP'
		);
关键是这种情况下我调用很多类都成功了,不是应该失败的吗,下面才是正确写法
		return array(
				'M'=>MODELS, 'V'=>VIEWS,
				'C'=>CTRLS, 'L'=>LIBS,
				'P'=>PLNS, 'H'=>HLP
		);
 [2012-09-13 06:15 UTC] laruence@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

到现在, 我确认这个和require once没什么关系了, 

也许你的宏本身就没定义好, 或者其他的什么原因造成的看起来怪异的结论, 

关闭这个报告,  建议你多打调试信息, 多关注错误输出去排查.

thanks
 [2012-09-13 06:15 UTC] laruence@php.net
-Status: Feedback +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 14:01:29 2024 UTC