php中__CLASS__与get_class 的区别

<?php
/*
*  Author : Fanglor
*  Email  : Fanlor@163.com
*  Url    : www.skyleft.com
*  Date   : 2011-06-28 
*/
class Test__CLASS__ {
 
	var $url = '';
 
	function __construct () {
		$this->url = __CLASS__;
	}
 
	function get_url () {
		return $this->url;
	}
}
 
class ChildTest__CLASS__ extends Test__CLASS__{
 
	function __construct () {
		parent::__construct ();
	}
}
 
//测试
$Test =new ChildTest__CLASS__(); 
echo $Test->url;
echo "<br/>";
 
class Test_get_class {
 
	var $url = '';
 
	function __construct () {
		$this->url = get_class($this);
	}
 
	function get_url () {
		return $this->url;
	}
}
 
class ChildTest_get_class extends Test_get_class{
 
	function __construct () {
		parent::__construct ();
	}
}
 
//测试
$Test =new ChildTest_get_class(); 
echo $Test->url;

测试结果 :

Test__CLASS__
ChildTest_get_class

可以看出,__CLASS__ 不向下传递,在哪个类里,就是哪类的名字,而 get_class 则更为灵活 ,与$this 结合使用,可以很方便的得出各子类的类名 。


1 Comment

御泥坊 on 六月 30, 2011 at 4:28 下午.

谢谢,正在找这段代码呢。

Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">

*
请输入图片中的字符以验证你并非垃圾机器人. 点击图片收听验证码的语音版.
点击这里收听此验证码的语音版本