Bug #63026 [Com]: require_once error

From: Date: Wed, 12 Sep 2012 01:56:19 +0000
Subject: Bug #63026 [Com]: require_once error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-173349@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63026&edit=1

 ID:                 63026
 Comment by:         ian dot xspace at yahoo dot cn
 Reported by:        ian dot xspace at yahoo dot cn
 Summary:            require_once error
 Status:             Feedback
 Type:               Bug
 Package:            *Compile Issues
 Operating System:   windows 7
 PHP Version:        5.3.16
 Block user comment: N
 Private report:     N

 New Comment:

//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

//大哥你要是再看不明白,我也没辙了


Previous Comments:
------------------------------------------------------------------------
[2012-09-12 01:48:03] 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-10 07:53:37] laruence@php.net

你用中文描述吧,
我实在看不懂你说什么错误.... 
另外,
请提供一个可以正常运行的可重试脚本.

(you can re-describe this problem in chinese, previous one is hard to read).

thanks

------------------------------------------------------------------------
[2012-09-10 07:44:44] 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-07 17:19:21] 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-07 05:28:30] 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



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63026&edit=1


Thread (12 messages)

« previous php.bugs (#173349) next »