函数名 :dirname
参数:文件路径
返回值:父目录路径。使用方法很简单:
require dirname(__FILE__).'/class.test.php';但是在类中使用为什么会报错呢?<?php
require_once dirname(__FILE__).'./test.php';
class testDirname{
public static $file_config = array(//下面会报错
'syn_file'=> dirname(__FILE__).'/data/syn_file',
'log_file'=> dirname(__FILE__).'/data/log_file',
'oper_file'=> dirname(__FILE__).'data/oper-file',
'test' => dirname('E:\www\dirname.php'),
);
public function getFileList(){
}
private function check(){
}
}
$file_config = array(//类之外使用是正确的。
'syn_file'=> dirname(__FILE__).'/data/syn_file',
'log_file'=> dirname(__FILE__).'/data/log_file',
'oper_file'=> dirname(__FILE__).'data/oper-file',
'test' => dirname('E:\www\dirname.php'),
);
print_r($file_config);
是什么原因呢?求解。。
参数:文件路径
返回值:父目录路径。使用方法很简单:
require dirname(__FILE__).'/class.test.php';但是在类中使用为什么会报错呢?<?php
require_once dirname(__FILE__).'./test.php';
class testDirname{
public static $file_config = array(//下面会报错
'syn_file'=> dirname(__FILE__).'/data/syn_file',
'log_file'=> dirname(__FILE__).'/data/log_file',
'oper_file'=> dirname(__FILE__).'data/oper-file',
'test' => dirname('E:\www\dirname.php'),
);
public function getFileList(){
}
private function check(){
}
}
$file_config = array(//类之外使用是正确的。
'syn_file'=> dirname(__FILE__).'/data/syn_file',
'log_file'=> dirname(__FILE__).'/data/log_file',
'oper_file'=> dirname(__FILE__).'data/oper-file',
'test' => dirname('E:\www\dirname.php'),
);
print_r($file_config);
是什么原因呢?求解。。
其它函数一样。不只是dirname。
常量 __FILE__ 要在运行时刻才产生你写成静态方法不是一样吗?只不过多了对括号题外话:
diname(__FILE__) 返回的是程序文件所在的目录,这样一来你的目录结构显得好奇怪的
一般类定义文件在网站目录结构中占据固定位置,似乎并不需要 diname(__FILE__) 吧?
把static去掉也是一样的。。
一样会错误,这个直接在类中这样使用也是错误的:
private $dir = dirname(__FILE__);
<?php
class testDirname{
public $file_config = addslashes('b');
}$t = new testDirname();
echo $t -> file_config;
你写个函数来set一下吧。或在类__construct中set一下。
class testDirname{
public $file_config = '0_0'; function __construct()
{
$this->file_config = dirname(__FILE__).'/data/log_file';
}
}$t = new testDirname();
echo $t -> file_config;
恩,我知道在construct中设置是可以的,set也是可以的,因为他们是在运行时被赋值的。只是属性的初始化这么用的话会错误的。
不过不是dirname问题。也不是__FILE__问题。
恩,唠叨大哥解释后,大约就知道了。之前一直以为是dirname 和__FILE__在类中因为没有实例化类,所以会出错呢,后来测试了下发现__construct中可以使用,也测试了下set和在其他的方法中使用,发现都没有问题。以为这个一个bug呢。所以才会以为是dirname的原因。总结的说就是唠叨大哥那句话:
类属性在定义时不能赋予不确定的值,谢谢大家。