成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

PHP變量與類型擴(kuò)展之類與對(duì)象

瀏覽:3日期:2022-09-16 09:31:27
一、概述和安裝

這些函數(shù)允許你獲得類和對(duì)象實(shí)例的相關(guān)信息。 你可以獲取對(duì)象所屬的類名,也可以是它的成員屬性和方法。 通過使用這些函數(shù),你不僅可以找到對(duì)象和類的關(guān)系,也可以是它們的繼承關(guān)系(例如,對(duì)象類繼承自哪個(gè)類)。

請(qǐng)參考面向?qū)ο笙嚓P(guān)章節(jié)來查看在 PHP 里,對(duì)象和類如何實(shí)現(xiàn)和使用的詳細(xì)描述。

使用這些函數(shù)不需要安裝,它們是 PHP 核心的一部分。

二、類與對(duì)象函數(shù)大全__autoload?— 嘗試加載未定義的類call_user_method_array?— 調(diào)用一個(gè)用戶方法,同時(shí)傳遞參數(shù)數(shù)組(已廢棄)call_user_method?— 對(duì)特定對(duì)象調(diào)用用戶方法(已廢棄)class_alias?— 為一個(gè)類創(chuàng)建別名class_exists?— 檢查類是否已定義get_called_class?— 后期靜態(tài)綁定('Late Static Binding')類的名稱get_class_methods?— 返回由類的方法名組成的數(shù)組get_class_vars?— 返回由類的默認(rèn)屬性組成的數(shù)組get_class?— 返回對(duì)象的類名get_declared_classes?— 返回由已定義類的名字所組成的數(shù)組get_declared_interfaces?— 返回一個(gè)數(shù)組包含所有已聲明的接口get_declared_traits?— 返回所有已定義的 traits 的數(shù)組get_object_vars?— 返回由對(duì)象屬性組成的關(guān)聯(lián)數(shù)組get_parent_class?— 返回對(duì)象或類的父類名interface_exists?— 檢查接口是否已被定義is_a?— 如果對(duì)象屬于該類或該類是此對(duì)象的父類則返回 TRUEis_subclass_of?— 如果此對(duì)象是該類的子類,則返回 TRUEmethod_exists?— 檢查類的方法是否存在property_exists?— 檢查對(duì)象或類是否具有該屬性trait_exists?— 檢查指定的 trait 是否存在三、使用范例

在這個(gè)例子中,我們首先定義了一個(gè)基類和該類的擴(kuò)展。 這個(gè)基類描述了普通的蔬菜,關(guān)于它是否可食用及其顏色。 子類?Spinach?添加了烹飪的方法和另一個(gè)檢查是否已烹飪的方法。

Example #1 classes.inc

<?php // base class with member properties and methods class Vegetable {var $edible;var $color;function Vegetable($edible, $color='green') { $this->edible = $edible; $this->color = $color;}function is_edible() { return $this->edible;}function what_color() { return $this->color;} } // end of class Vegetable // extends the base class class Spinach extends Vegetable { var $cooked = false; function Spinach(){ $this->Vegetable(true, 'green'); } function cook_it(){ $this->cooked = true; } function is_cooked(){ return $this->cooked; } } // end of class Spinach?>

接下來我們從這些類中實(shí)例化了兩個(gè)對(duì)象,并打印了他們的信息,包括了他們類的繼承關(guān)系。 同時(shí)我們也定了一些實(shí)用函數(shù),主要為了漂亮地打印出這些變量。

Example #2 test_script.php

<?php include 'classes.inc'; // 實(shí)用函數(shù) function print_vars($obj) {foreach (get_object_vars($obj) as $prop => $val) { echo 't$prop = $valn';} } function print_methods($obj) {$arr = get_class_methods(get_class($obj));foreach ($arr as $method) { echo 'tfunction $method()n';} } function class_parentage($obj, $class) {if (is_subclass_of($GLOBALS[$obj], $class)) { echo 'Object $obj belongs to class ' . get_class($$obj); echo ' a subclass of $classn';} else { echo 'Object $obj does not belong to a subclass of $classn';} } // 實(shí)例化 2 對(duì)象 $veggie = new Vegetable(true, 'blue'); $leafy = new Spinach(); // 打印這些對(duì)象的信息 echo 'veggie: CLASS ' . get_class($veggie) . 'n'; echo 'leafy: CLASS ' . get_class($leafy); echo ', PARENT ' . get_parent_class($leafy) . 'n'; // 顯示蔬菜的屬性 echo 'nveggie: Propertiesn'; print_vars($veggie); // and leafy methods echo 'nleafy: Methodsn'; print_methods($leafy); echo 'nParentage:n'; class_parentage('leafy', 'Spinach'); class_parentage('leafy', 'Vegetable');?>

一個(gè)重要的東西是注意在上面的例子中,對(duì)象?$leafy?是?Spinach?的實(shí)例(Vegetable?的子類),另外腳本的最后部分會(huì)輸出以下信息:

[...]Parentage:Object leafy does not belong to a subclass of SpinachObject leafy belongs to class spinach a subclass of Vegetable

標(biāo)簽: PHP
相關(guān)文章: