您的位置 首页 php

php类与对象的函数一

class_exists()检查类是否已定义

格式bool class_exists(str $class_name[, bool $autoload])

5.0.2版后,不再为已定义的接口返回TRUE。请使用interface_exists()。如:

if (!class_exists($class, FALSE )) {

trigger_error(“Unable to load class: $class”, E_USER_WARNING);

}

get_class()返回对象的类名

格式string get_class([object $obj])

abstract class bar {

public function __construct(){

var_dump(get_class($this));

var_dump(get_class());

}

}

class foo extends bar {}

new foo;

输出:

string(3) “foo”

string(3) “bar”

get_class_methods()返回类的方法名组成的数组

类外不能返回私有方法

格式:array get_class_methods(mixed $class_name)

class my_class{

public function method1(){}

private function method2(){}

};

print_r(get_class_methods(‘my_class’));//数组,method1

get_object_vars()返回对象属性组成的关联数组

格式: array get_objece_vars(object $obj)

class foo {

private $a;

public $b = 1;

public $c;

private $d;

static $e;

public function test(){

var_dump(get_object_vars($this));

}

}

$test = new foo;

var_dump(get_object_vars($test));

$test->test();

输出
array(2) {

[“b”]=>int(1)

[“c”]=>NULL

}

array(4) {

[“a”]=>NULL

[“b”]=>int(1)

[“c”]=>NULL

[“d”]=>NULL

}

get_parent_class()返回对象或者类的父类名

格式:string get_parent_class([mixed $obj])

class dad {

function dad(){}

}

class child extends dad {

function child(){

echo “I’m ” , get_parent_class($this) , “‘s son\n”;

}

}

$foo = new child();//I’m dad’s son

is_a()同 instanceof ()

格式:bool is_a(object $object,str $class_name[, bool $allow_string = FALSE ])

$WF = new WFactory();

is_a($WF,’WFactory’)等同于$WF instanceof WFactory

method_exists()检查类的方法是否存在

格式:bool method_exists($object,$method_name)

$directory = new Directory(‘.’);

var_dump(method_exists($directory,’read’));//true

var_dump(method_exists(‘Directory’,’read’));//true

property_exists()检查对象或类是否具有该属性

格式:bool property_exists(mixed $class,str $property)

$directory = new Directory(‘.’)

class myClass {

public $mine;

private $xpto;

static protected $test;

static function test() {

var_dump(property_exists(‘myClass’, ‘xpto’)); //true

}

}

var_dump(property_exists(‘myClass’, ‘mine’)); //true

var_dump(property_exists(new myClass, ‘xpto’)); //true, as of PHP 5.3.0

var_dump(property_exists(‘myClass’, ‘test’)); //true, as of PHP 5.3.0

myClass::test();

get_declared_classes()返回所有已定义的类的名字组成的数组

格式:array get_declared_classes ( void )

print_r(get_declared_classes());

__autoload(php7.2弃用)

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

文章标题:php类与对象的函数一

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

关于作者: 智云科技

热门文章

网站地图