查找类中属性和方法
- class_exists($classname_string);//bool
- get_class($object);//string
- get_parent_class($obj || $classname);//string
- get_class_vars($classname);//array
- get_class_methods($classname);//array 仅返回可见性为public的方法(新版本)
- method_exists($obj || $classname, $method);//bool 所有可见性均可判断
- is_callable()
self $this static[非修饰符] parent的区别
- self代表类本身 self::跟静态的方法、属性或类中常量
- $this 代表此对象
- parent 父类
- static 用法类似self。若static和self都在父类中,子类调用时,static会代表子类,而self依旧代表父类。
<?php
class Dad
{
public static function whoAmI()
{
return 'I am '.__CLASS__;
}
public static function testSelf()
{
echo 'self: '.self::whoAmI().'<br>';
}
public static function testStatic()
{
echo 'static: '.static::whoAmI().'<br>';
}
}
class Son1 extends Dad
{
public static function whoAmI()
{
return 'rewrite: I am '.__CLASS__;
}
}
class Son2 extends Dad
{
//重写父类的方法
//调用testStatic()将调子类重写后的方法
//调用testSelf()将依旧调用父类的whoAmI()
public static function whoAmI()
{
return 'rewrite: I am '.__CLASS__;
}
}
Son1::testSelf();//echo self: I am Dad
Son1::testStatic();//echo static: I am Son1
echo '<hr>';
Son2::testSelf();//echo self: I am Dad
Son2::testStatic();//echo static: I am Son2
网友评论