


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)鍵字:PHP、函數(shù)、實例
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認iptable規(guī)則詳解