您的位置 首页 php

php操作类与对象的函数二

class_alias为类创建一个别名

格式:bool class_alias(str $original,str $alias[, bool $autoload=TRUE])

class foo { }

class_alias(‘foo’, ‘bar’);

此时foo与bar这两个类一样

get_called_class获取类的名称, 静态方法 被调用,无参数,类内使用

格式:string get_called_class ( void )

class foo {

static public function test() {

var_dump( get_called_class() );

}

}

class bar extends foo {}

foo::test();//foo

bar::test();//bar

get_class_ var s获取可以从当前范围访问的类的默认属性

格式:array get_class_vars ( string $class_name )

class myclass {

var $var1; //无默认值

var $var2 = “xyz”;

var $var3 = 100;

private $var4;

function __construct() {

// change some properties

$this->var1 = “foo”;

$this->var2 = “bar”;

return true;

}

}

$my_class = new myclass();

print_r( get_class_vars(get_class($my_class)) );

print_r( get_object_vars($my_class) );

输出:

Array(

[var1] =>

[var2] => xyz

[var3] => 100

)

Array(

[var1] => foo

[var2] => bar

[var3] => 100

)

get_declared_interfaces返回所有已声明的接口的数组

格式:array get_declared_interfaces ( void )

interface haha{}

print_r(get_declared_interfaces());

get_declared_traits返回所有已声明为trait的数组

格式:array get_declared_traits ( void )

trait Trait1 {

public function hello() {}

}

trait Trait2 {

public function hi() {}

}

print_r(get_declared_traits());

interface_exists检查接口是否已定义

格式:bool interface_exists (str $interface_name [,bool $autoload = true ] )

if (interface_exists(‘Myface’)) {

class MyClass implements Myface

{ // Methods }

}

is_subclass_of检查对象是否有这个类作为它的一个父类或实现它

格式:bool is_subclass_of ( mixed $object , string $class_name [, bool $allow_string = TRUE ] )

class Factory {

var $oink = ‘father’;

}

class F_Child extends Factory{

var $oink = ‘child’;

}

if (is_subclass_of(‘F_Child’, ‘Factory’)) {

echo “是, F_Child 有父类 Factory\n”;

} else {

echo “不, F_Child 没有父类Factory\n”;

}

输出:是, F_Child 有父类 Factory

trait_exists检查trait类是否存在

格式:bool trait_exists ( string $traitname [, bool $autoload ] )

trait World {

private static $instance;

protected $ tmp ;

public static function World(){

//将(hello)对象赋值给静态属性

self::$instance = new static();

self::$instance->tmp = get_called_class().’ ‘.__TRAIT__;

return self::$instance;//返回一个对象

}

}

if ( trait_exists( ‘World’ ) ) {

class Hello {

use World;

public function text( $str ){

return $this->tmp.$str;

}

}

}

echo Hello::World()->text(‘!!!’); // Hello World!!!

文章来源:智云一二三科技

文章标题:php操作类与对象的函数二

文章地址:https://www.zhihuclub.com/76643.shtml

关于作者: 智云科技

热门文章

网站地图