PHP類和對象函數實例詳解

1. interface_exists、class_exists、method_exists和property_exists:php

      顧名思義,從以上幾個函數的命名即可以猜出幾分他們的功能。我想這也是我隨着對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
//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";
} 

    運行結果以下: 數組

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: 編程語言

    分別返回當前能夠訪問的全部類和接口,這不只包括自定義類和接口,也包括了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());

    因爲輸出結果過長,並且這兩個函數也比較簡單,因此下面就再也不給出輸出結果了。學習

3. get_class_methods、get_class_vars和get_object_vars: 測試

    這三個函數有一個共同點,即只能獲取做用域可見範圍內的全部成員函數、成員變量或非靜態成員變量。好比在類的內部調用,則全部成員函數或者變量都符合條件,而在類的外部,則只有共有的函數和變量能夠返回。
array get_class_methods (mixed $class_name) 獲取指定類中可訪問的成員函數。
array get_class_vars (string $class_name) 獲取指定類中能夠訪問的成員變量。
array get_object_vars (object $object) 獲取能夠訪問的非靜態成員變量。ui

<?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));

    運行結果以下:this

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

4. get_called_class和get_class:spa

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()));

    運行結果以下:

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

5. 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)
相關文章
相關標籤/搜索