快捷搜索:

类相关函数的利用详解,PHP类和指标函数实例详

作者: w88官方网站手机版  发布:2019-08-17

bool class_alias ( string $original , string $alias [, bool $autoload = TRUE ] ) — 为三个类创立别名
bool class_exists ( string $class_name [, bool $autoload ] )— 检查类是或不是已定义
string get_called_class ( void ) —获取静态方法调用的类名

1. interface_exists、class_exists、method_exists和property_exists:

  1. interface_exists、class_exists、method_exists和property_exists:

复制代码 代码如下:

      从名称想到所包含的意义,从上述多少个函数的命名便足以猜出几分他们的功能。小编想那也是自己随着对PHP的入木八分学习而更是喜欢那门编制程序语言的案由了吗。下边先付给他们的原型证明和精炼说明,越多的依然直接看例子代码吧。
bool interface_exists (string $interface_name [, bool $autoload = true ]) 剖断接口是不是存在,第三个参数表示在追寻时是还是不是实行__autoload。
bool class_exists (string $class_name [, bool $autoload = true ]) 剖断类是或不是存在,第三个参数表示在查究时是否实践__autoload。
bool method_exists (mixed $object , string $method_name) 判定钦定类或许指标中是不是带有钦定的分子函数。
bool property_exists (mixed $class , string $property) 判别内定类大概目的中是或不是带有钦命的分子变量。

      从名称想到所包含的意义,从以上几个函数的命名便能够猜出几分他们的作用。作者想那也是本人趁着对PHP的深远学习而尤为喜欢那门编制程序语言的原故了吧。下边先付给他们的原型注脚和轻易表达,更加多的要么一直看例子代码吧。
bool interface_exists (string $interface_name [, bool $autoload = true ]) 推断接口是还是不是留存,第四个参数表示在寻觅时是还是不是进行__autoload。
bool class_exists (string $class_name [, bool $autoload = true ]) 判定类是或不是留存,首个参数表示在寻觅时是或不是施行__autoload。
bool method_exists (mixed $object , string $method_name) 推断钦赐类或然指标中是或不是含有钦赐的积极分子函数。
bool property_exists (mixed $class , string $property) 决断钦定类大概指标中是不是包涵钦命的成员变量。

class foo {
    static public function test(){
        var_dump(get_called_class());
    }
}
class bar extends foo {}
foo::test();
bar::test();

<?php
//in another_test_class.php
interface AnotherTestInterface {

}

class AnotherTestClass {
    public static function printMe() {
        print "This is Test2::printSelf.n";
    }
    public function doSomething() {
        print "This is Test2::doSomething.n";
    }
    public function doSomethingWithArgs($arg1, $arg2) {
        print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").n";
    }
}

<?php
//in class_exist_test.php, 下面测试代码中所需的类和接口位于another_test_class.php,
//由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。
//这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。
function __autoload($classname) {
    $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname));
    require strtolower($nomilizedClassname).".php";
}
//spl_autoload_register(function($classname) {
//    $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname));
//    require strtolower($nomilizedClassname).".php";
//});

print "The following case is tested before executing autoload.n";
if (!class_exists('AnotherTestClass',false)) {
    print "This class doesn't exist if no autoload.n";
}

if (!interface_exists('AnotherTestInterface',false)) {
    print "This interface doesn't exist if no autoload.n";
}

print "nThe following case is tested after executing autoload.n";
if (class_exists('AnotherTestClass',true)) {
    print "This class exists if autoload is set to true.n";
}

if (interface_exists('AnotherTestInterface',true)) {
    print "This interface exists if autoload is set to true.n";
} 

 代码如下

array get_class_methods ( mixed $class_name )— 再次来到由类的点子名组成的数组
array get_class_vars ( string $class_name )— 重返由类的暗中同意属性组成的数组
string get_class ([ object $obj ] )— 重回对象的类名
array get_declared_classes ( void )— 重返当前剧本中已定义类的名字组成的数组
array get_declared_interfaces ( void )— 再次回到当前剧本中负有已表明的接口的名字数组
array get_object_vars ( object $obj )— 再次来到由对象属性组成的涉嫌数组
string get_parent_class ([ mixed $obj ] )— 再次回到对象或类的父类名
bool interface_exists ( string $interface_name [, bool $autoload ] )— 检查接口是还是不是已被定义
bool is_a ( object $object , string $class_name )— 假若目的属于此类或该类是此目的的父类则赶回 TRUE
bool is_subclass_of ( object $object , string $class_name )— 检测对象是此类的子类实例化得到的
bool method_exists ( object $object , string $method_name )— 检查类的格局是或不是留存
bool property_exists ( mixed $class , string $property ) — 检核查象或类是或不是持有该属性

    运转结果如下: 

<?php
//in another_test_class.php
interface AnotherTestInterface {

您恐怕感兴趣的小说:

  • PHP类和对象相关系统函数与运算符小结
  • 浅谈PHP中类和目的的连锁函数
bogon:TestPhp$ php class_exist_test.php 
The following case is tested before executing autoload.
This class doesn't exist if no autoload.
This interface doesn't exist if no autoload.

The following case is tested after executing autoload.
This class exists if autoload is set to true.
This interface exists if autoload is set to true.

}

2. get_declared_classes和get_declared_interfaces: 

class AnotherTestClass {
    public static function printMe() {
        print "This is Test2::printSelf.n";
    }
    public function doSomething() {
        print "This is Test2::doSomething.n";
    }
    public function doSomethingWithArgs($arg1, $arg2) {
        print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").n";
    }
}

    分别重临当前得以访问的全部类和接口,那不仅仅包罗自定义类和接口,也囊括了PHP内置类和接口。他们的函数证明极其轻松,未有参数,只是再次来到数组。见如下代码:

<?php
//in class_exist_test.php, 上面测验代码中所需的类和接口位于another_test_class.php,
//因此能够开采规律,类和接口的名目是驼峰风格的,而文件名的单词间是下划线分隔的。
//这里给出了三种__autoload的办法,因为第一种特别常用和有助于,因而大家那边将第二种格局讲明掉了,他们中间的不一样能够查阅manual。
function __autoload($classname) {
    $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname));
    require strtolower($nomilizedClassname).".php";
}
//spl_autoload_register(function($classname) {
//    $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname));
//    require strtolower($nomilizedClassname).".php";
//});

<?php
interface AnotherTestInterface {

}

class AnotherTestClass {
    public static function printMe() {
        print "This is Test2::printSelf.n";
    }
}

print_r(get_declared_interfaces());
print_r(get_declared_classes());

print "The following case is tested before executing autoload.n";
if (!class_exists('AnotherTestClass',false)) {
    print "This class doesn't exist if no autoload.n";
}

    由于出口结果过长,并且这两个函数也相比轻松,所以下边就不再给出输出结果了。

if (!interface_exists('AnotherTestInterface',false)) {
    print "This interface doesn't exist if no autoload.n";
}

3. get_class_methods、get_class_vars和get_object_vars: 

print "nThe following case is tested after executing autoload.n";
if (class_exists('AnotherTestClass',true)) {
    print "This class exists if autoload is set to true.n";
}

    那八个函数有多少个共同点,即只可以获得功能域可见范围内的具有成员函数、成员变量或非静态成员变量。比如在类的个中调用,则有着成员函数也许变量都符合条件,而在类的表面,则唯有共有的函数和变量能够回到。
array get_class_methods (mixed $class_name) 获取钦点类中可访谈的分子函数。
array get_class_vars (string $class_name) 获取钦定类中得以访谈的分子变量。
array get_object_vars (object $object) 获取能够访谈的非静态成员变量。

if (interface_exists('AnotherTestInterface',true)) {
    print "This interface exists if autoload is set to true.n";
}    

<?php
function output_array($functionName, $items) {
    print "$functionName.....................n";
    foreach ($items as $key => $value) {
        print '$key = '.$key. ' => $value = '.$value."n";
    }
}

class TestClass {
    public $publicVar = 1;
    private $privateVar = 2;
    static private $staticPrivateVar = "hello";
    static public $staticPublicVar;

    private function privateFunction() {

    }
    function publicFunction() {
        output_array("get_class_methods",get_class_methods(__CLASS__));
        output_array('get_class_vars',get_class_vars(__CLASS__));
        output_array('get_object_vars',get_object_vars($this));
    }
}

$testObj = new TestClass();
print "The following is output within TestClass.n";
$testObj->publicFunction();

print "nThe following is output out of TestClass.n";
output_array('get_class_methods',get_class_methods('TestClass'));
output_array('get_class_vars',get_class_vars('TestClass'));
output_array('get_object_vars',get_object_vars($testObj));

 运行结果如下:

    运维结果如下:

bogon:TestPhp$ php class_exist_test.php
The following case is tested before executing autoload.
This class doesn't exist if no autoload.
This interface doesn't exist if no autoload.

bogon:TestPhp liulei$ php class_exist_test.php 
The following is output within TestClass.
get_class_methods.....................
$key = 0 => $value = privateFunction
$key = 1 => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value = 1
$key = privateVar => $value = 2
$key = staticPrivateVar => $value = hello
$key = staticPublicVar => $value = 
get_object_vars.....................
$key = publicVar => $value = 1
$key = privateVar => $value = 2

The following is output out of TestClass.
get_class_methods.....................
$key = 0 => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value = 1
$key = staticPublicVar => $value = 
get_object_vars.....................
$key = publicVar => $value = 1

The following case is tested after executing autoload.
This class exists if autoload is set to true.
This interface exists if autoload is set to true.2. get_declared_classes和get_declared_interfaces:

4. get_called_class和get_class:

    分别重临当前得以访问的全部类和接口,那不但满含自定义类和接口,也囊括了PHP内置类和接口。他们的函数评释特别简单,未有参数,只是重临数组。见如下代码:

string get_class ([ object $object = NULL ]) 获取参数对象的类名称。
string get_called_class (void) 静态方法调用时当前的类名称。

 代码如下

<?php
class Base {
    static public function test() {
        var_dump(get_called_class());
    }
}

class Derive extends Base {
}

Base::test();
Derive::test();

var_dump(get_class(new Base()));
var_dump(get_class(new Derive()));

<?php
interface AnotherTestInterface {

    运营结果如下:

}

bogon:TestPhp$ php another_test_class.php 
string(4) "Base"
string(6) "Derive"
string(4) "Base"
string(6) "Derive"

class AnotherTestClass {
    public static function printMe() {
        print "This is Test2::printSelf.n";
    }
}

5. get_parent_class、is_a和is_subclass_of:

print_r(get_declared_interfaces());
print_r(get_declared_classes());   

    那多少个函数都是和类的承接相关,所以本人把她们归到了一块儿。

由于出口结果过长,何况那四个函数也比较轻易,所以上面就不再给出输出结果了。

string get_parent_class ([ mixed $object ]) 获取参数对象的父类,若无父类则赶回false。
bool is_a (object $object, string $class_name) 决断第一个参数对象是不是是$class_name类本人也许其父类的对象。
bool is_subclass_of (mixed $object, string $class_name) 判断第三个参数对象是或不是是$class_name的子类。

  1. get_class_methods、get_class_vars和get_object_vars:
<?php
class Base {
    static public function test() {
        var_dump(get_called_class());
    }
}

class Derive extends Base {
}

var_dump(get_parent_class(new Derive()));
var_dump(is_a(new Derive(),'Derive'));
var_dump(is_a(new Derive(),'Base'));
var_dump(is_a(new Base(),'Derive'));

var_dump(is_subclass_of(new Derive(),'Derive'));
var_dump(is_subclass_of(new Derive(),'Base'));

    这八个函数有贰个共同点,即只可以得到成效域可知范围内的保有成员函数、成员变量或非静态成员变量。例如在类的内部调用,则兼具成员函数可能变量都符合条件,而在类的外表,则独有共有的函数和变量能够回来。
array get_class_methods (mixed $class_name) 获取内定类中可访谈的成员函数。
array get_class_vars (string $class_name) 获取钦赐类中得以访谈的成员变量。
array get_object_vars (object $object) 获取能够访问的非静态成员变量。

    运维结果如下:

 代码如下

bogon:TestPhp$ php another_test_class.php 
string(4) "Base"
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)

<?php
function output_array($functionName, $items) {
    print "$functionName.....................n";
    foreach ($items as $key => $value) {
        print '$key = '.$key. ' => $value = '.$value."n";
    }
}

class TestClass {
    public $publicVar = 1;
    private $privateVar = 2;
    static private $staticPrivateVar = "hello";
    static public $staticPublicVar;

    private function privateFunction() {

    }
    function publicFunction() {
        output_array("get_class_methods",get_class_methods(__CLASS__));
        output_array('get_class_vars',get_class_vars(__CLASS__));
        output_array('get_object_vars',get_object_vars($this));
    }
}

$testObj = new TestClass();
print "The following is output within TestClass.n";
$testObj->publicFunction();

print "nThe following is output out of TestClass.n";
output_array('get_class_methods',get_class_methods('TestClass'));
output_array('get_class_vars',get_class_vars('TestClass'));
output_array('get_object_vars',get_object_vars($testObj));    运维结果如下:

bogon:TestPhp liulei$ php class_exist_test.php
The following is output within TestClass.
get_class_methods.....................
$key = 0 => $value = privateFunction
$key = 1 => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value = 1
$key = privateVar => $value = 2
$key = staticPrivateVar => $value = hello
$key = staticPublicVar => $value =
get_object_vars.....................
$key = publicVar => $value = 1
$key = privateVar => $value = 2

The following is output out of TestClass.
get_class_methods.....................
$key = 0 => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value = 1
$key = staticPublicVar => $value =
get_object_vars.....................
$key = publicVar => $value = 14. get_called_class和get_class:

string get_class ([ object $object = NULL ])  www.111Cn.net获取参数对象的类名称。
string get_called_class (void) 静态方法调用时当前的类名称。

<?php
class Base {
    static public function test() {
        var_dump(get_called_class());
    }
}

class Derive extends Base {
}

Base::test();
Derive::test();

var_dump(get_class(new Base()));
var_dump(get_class(new Derive()));   

运作结果如下:

bogon:TestPhp$ php another_test_class.php
string(4) "Base"
string(6) "Derive"
string(4) "Base"
string(6) "Derive"

  1. get_parent_class、is_a和is_subclass_of:

    那四个函数都是和类的一连相关,所以自身把她们归到了伙同。

string get_parent_class ([ mixed $object ]) 获取参数对象的父类,若无父类则赶回false。
bool is_a (object $object, string $class_name) 推断第叁个参数对象是不是是$class_name类本人大概其父类的靶子。
bool is_subclass_of (mixed $object, string $class_name) 剖断第二个参数对象是或不是是$class_name的子类。

 

 代码如下

<?php
class Base {
    static public function test() {
        var_dump(get_called_class());
    }
}

class Derive extends Base {
}

var_dump(get_parent_class(new Derive()));
var_dump(is_a(new Derive(),'Derive'));
var_dump(is_a(new Derive(),'Base'));
var_dump(is_a(new Base(),'Derive'));

var_dump(is_subclass_of(new Derive(),'Derive'));
var_dump(is_subclass_of(new Derive(),'Base'));    运营结果如下:

bogon:TestPhp$ php another_test_class.php
string(4) "Base"
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)

本文由www.w88985.com发布于w88官方网站手机版,转载请注明出处:类相关函数的利用详解,PHP类和指标函数实例详

关键词: www.w88985.c