亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網(wǎng)絡(luò),我們將竭誠為您提供優(yōu)質(zhì)的服務(wù)! 誠征網(wǎng)絡(luò)推廣 | 網(wǎng)站備案 | 幫助中心 | 軟件下載 | 購買流程 | 付款方式 | 聯(lián)系我們 [ 會員登錄/注冊 ]
促銷推廣
客服中心
業(yè)務(wù)咨詢
有事點擊這里…  531199185
有事點擊這里…  61352289
點擊這里給我發(fā)消息  81721488
有事點擊這里…  376585780
有事點擊這里…  872642803
有事點擊這里…  459248018
有事點擊這里…  61352288
有事點擊這里…  380791050
技術(shù)支持
有事點擊這里…  714236853
有事點擊這里…  719304487
有事點擊這里…  1208894568
有事點擊這里…  61352289
在線客服
有事點擊這里…  531199185
有事點擊這里…  61352288
有事點擊這里…  983054746
有事點擊這里…  893984210
當前位置:首頁 >> 技術(shù)文章 >> 文章瀏覽
技術(shù)文章

PHP類和對象函數(shù)實例詳解

添加時間:2014-6-27 17:49:52  添加: 思海網(wǎng)絡(luò) 
1. interface_exists、class_exists、method_exists和property_exists:
 
      顧名思義,從以上幾個函數(shù)的命名便可以猜出幾分他們的功能。我想這也是我隨著對PHP的深入學習而越來越喜歡這門編程語言的原因了吧。下面先給出他們的原型聲明和簡短說明,更多的還是直接看例子代碼吧。
bool interface_exists (string $interface_name [, bool $autoload = true ]) 判斷接口是否存在,第二個參數(shù)表示在查找時是否執(zhí)行__autoload。
bool class_exists (string $class_name [, bool $autoload = true ]) 判斷類是否存在,第二個參數(shù)表示在查找時是否執(zhí)行__autoload。
bool method_exists (mixed $object , string $method_name) 判斷指定類或者對象中是否含有指定的成員函數(shù)。
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,
//由此可以發(fā)現(xiàn)規(guī)律,類和接口的名稱是駝峰風格的,而文件名的單詞間是下劃線分隔的。
//這里給出了兩種__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";
 
    運行結(jié)果如下: 
 
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.
 
2. get_declared_classes和get_declared_interfaces: 
 
    分別返回當前可以訪問的所有類和接口,這不僅包括自定義類和接口,也包括了PHP內(nèi)置類和接口。他們的函數(shù)聲明非常簡單,沒有參數(shù),只是返回數(shù)組。見如下代碼:
 
<?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());
 
    由于輸出結(jié)果過長,而且這兩個函數(shù)也比較簡單,所以下面就不再給出輸出結(jié)果了。
 
3. get_class_methods、get_class_vars和get_object_vars: 
 
    這三個函數(shù)有一個共同點,即只能獲取作用域可見范圍內(nèi)的所有成員函數(shù)、成員變量或非靜態(tài)成員變量。比如在類的內(nèi)部調(diào)用,則所有成員函數(shù)或者變量都符合條件,而在類的外部,則只有共有的函數(shù)和變量可以返回。
array get_class_methods (mixed $class_name) 獲取指定類中可訪問的成員函數(shù)。
array get_class_vars (string $class_name) 獲取指定類中可以訪問的成員變量。
array get_object_vars (object $object) 獲取可以訪問的非靜態(tài)成員變量。
 
<?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));
 
    運行結(jié)果如下:
 
 
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:
 
string get_class ([ object $object = NULL ]) 獲取參數(shù)對象的類名稱。
string get_called_class (void) 靜態(tài)方法調(diào)用時當前的類名稱。
 
 
<?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()));
 
    運行結(jié)果如下:
 
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:
 
    這三個函數(shù)都是和類的繼承相關(guān),所以我把他們歸到了一起。
 
string get_parent_class ([ mixed $object ]) 獲取參數(shù)對象的父類,如果沒有父類則返回false。
bool is_a (object $object, string $class_name) 判斷第一個參數(shù)對象是否是$class_name類本身或是其父類的對象。
bool is_subclass_of (mixed $object, string $class_name) 判斷第一個參數(shù)對象是否是$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'));
 
    運行結(jié)果如下:
 
 
bogon:TestPhp$ php another_test_class.php 
string(4) "Base"
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
關(guān)鍵字:PHP、函數(shù)、實例
分享到:

頂部 】 【 關(guān)閉
版權(quán)所有:佛山思海電腦網(wǎng)絡(luò)有限公司 ©1998-2024 All Rights Reserved.
聯(lián)系電話:(0757)22630313、22633833
中華人民共和國增值電信業(yè)務(wù)經(jīng)營許可證: 粵B1.B2-20030321 備案號:粵B2-20030321-1
網(wǎng)站公安備案編號:44060602000007 交互式欄目專項備案編號:200303DD003  
察察 工商 網(wǎng)安 舉報有獎  警警  手機打開網(wǎng)站