《php類(lèi)與對(duì)象的函數(shù)一》要點(diǎn):
本文介紹了php類(lèi)與對(duì)象的函數(shù)一,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
歡迎參與《php類(lèi)與對(duì)象的函數(shù)一》討論,分享您的想法,維易PHP學(xué)院為您提供專(zhuān)業(yè)教程。
class_exists()檢查類(lèi)是否已定義
格式bool class_exists(str $class_name[,bool $autoload])
5.0.2版后,不再為已定義的接口返回TRUE.請(qǐng)使用interface_exists().如:
if (!class_exists($class, false)) {
trigger_error("Unable to load class: $class", E_USER_WARNING);
}
get_class()返回對(duì)象的類(lèi)名
格式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()返回類(lèi)的方法名組成的數(shù)組
類(lèi)外不能返回私有方法
格式:array get_class_methods(mixed $class_name)
class my_class{
public function method1(){}
private function method2(){}
};
print_r(get_class_methods('my_class'));//數(shù)組,method1
get_object_vars()返回對(duì)象屬性組成的關(guān)聯(lián)數(shù)組
格式: 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()返回對(duì)象或者類(lèi)的父類(lèi)名
格式: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()檢查類(lèi)的方法是否存在
格式: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()檢查對(duì)象或類(lèi)是否具有該屬性
格式: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()返回所有已定義的類(lèi)的名字組成的數(shù)組
格式:array get_declared_classes ( void )
print_r(get_declared_classes());
__autoload(php7.2棄用)
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/12411.html