新闻报价汽车商家技术软件 驱动     本站短域名:珠江路.cn、zjlu.net

电脑

数码

办公

网络

汽车

招聘

经销商

珠江路论坛

购物街
首页 新闻 专题 报价库 经销商 驱动 软件 游戏 学院
三大件 主板 显卡 显示器 装机推荐 珠江路渠道特价
手机 DC/DV GPS 笔记本 平板电脑
学院首页 软件应用 编程开发 创意设计
ASP ASP.NET PHP JSP SQL MYSQL Java VB DIV+CSS JavaScript XML

您的位置:电脑学院 >> 编程开发 >> PHP >> 再介绍几个与类/对象相关的新函数


再介绍几个与类/对象相关的新函数



上回我介绍了几个array库里的新增函数,现在再给大家介绍几个类/对象相关的函数吧,好象也是手册上没有的。

call_user_method — 调用一个方法,与“对象名->方法”一样了。
class_exists — 判断类是否存在
get_class — 取类的名称
get_class_methods — 取类的成员方法的名称
get_class_vars — 取类的成员变量的名称
get_declared_classes — 返回一个数组,包含所有定义的类的名称
get_object_vars — 取对象的属性
get_parent_class — 取父类
is_subclass_of — 判断一个类是否是另一个类的子类
method_exists — 判断一个成员方法是否存在

这些函数允许你得到一个特定的类的信息,如成员数据、成员方法等,甚至还可以得到它的基类的信息。

先举一个例子来说明一下吧。

首先定义一个基类,Vegetable,两个属性 edible,color。
然后继承一个子类,spinach,加上两个方法 cook,cooked。

Example 1. classes.inc

php

// 基类Vegetable,及成员属性方法等

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


// 子类Spinach

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

?>

定义了这样两个类,下面我们调用一些函数来显示出它们的信息吧。
先定义一些工作函数吧。

Example 2. test_script.php

 

include "classes.inc";

// utility functions

function print_vars($obj) {
$arr = get_object_vars($obj);//取类的变量!
while (list($prop, $val) = each($arr))
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) {
global $$obj;
if (is_subclass_of($$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";
}
}

// 声明两个实例对象

$veggie = new Vegetable(true,"blue");
$leafy = new Spinach();

// print out information about objects
echo "veggie: CLASS ".get_class($veggie)."n";
echo "leafy: CLASS ".get_class($leafy);
echo ", PARENT ".get_parent_class($leafy)."n";

// show veggie properties
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");
?>



好了,下面正式开始说明这几个函数。
格式与上一篇文章一相了。

//**********************

call_user_method (3.0.3 - 3.0.16 only, PHP4 )

mixed call_user_method (string method_name, object obj [, mixed parameter [, mixed ...]])

调用一个方法。方法名method_name,对象名 obj。
下面这个例子定义了一个类,实例化一个对象,用本函数直接调用它的一个print_info()方法。


class Country {
var $NAME;
var $TLD;

function Country($name, $tld) {
$this->NAME = $name;
$this->TLD = $tld;
}

function print_info($prestr="") {
echo $prestr."Country: ".$this->NAME."
";
echo $prestr."Top Level Domain: ".$this->TLD."
";
}
}

$cntry = new Country("Peru","pe");

echo "* Calling the object method directly
";
$cntry->print_info();

echo "
* Calling the same method indirectly
";
call_user_method ("print_info", $cntry, "t");
?>

//***************************
class_exists (PHP4 >= 4.0b4)

bool class_exists (string class_name)

如果类存在已被定义,返回真。

//*****************************
get_class (PHP4 >= 4.0b2)

string get_class (object obj)
返回obj的类名。obj是一个对象名。

//*****************************
get_class_methods (PHP4 >= 4.0RC1)

array get_class_methods (string class_name)

返回一个数组,包含定义的成员方法名称。

//******************************
get_class_vars (PHP4 >= 4.0RC1)

array get_class_vars (string class_name)

同上,返回一个数组,包含定义的成员变量名称。

//******************************
get_declared_classes (PHP4 >= 4.0RC2)

array get_declared_classes (void)

返回一个数组,是所定义的类的名称。

注意:在 PHP 4.0.1pl2 中,会在数组前返回三个额外的类名,stdClass(在Zend/zend.c 中定义),OverloadedTestClass (在 ext/standard/basic_functions.c 中定义) 和 Directory (在 ext/standard/dir.c 中定义).

//********************************
get_object_vars (PHP4 >= 4.0RC1)

array get_object_vars (object obj)

返回一个关联数组,包含对象实例的属性(key为名称,value为值)。

Example 1. Use of get_object_vars()

class Point2D {
var $x, $y;
var $label;

function Point2D($x, $y) {
$this->x = $x;
$this->y = $y;
}

function setLabel($label) {
$this->label = $label;
}

function getPoint() {
return array("x" => $this->x,
"y" => $this->y,
"label" => $this->label);
}
}

$p1 = new Point2D(1.233, 3.445);
print_r(get_object_vars($p1));
// "$label" is declared but not defined
// Array
// (
// [x] => 1.233
// [y] => 3.445
// )

$p1->setLabel("point #1");
print_r(get_object_vars($p1));
// Array
// (
// [x] => 1.233
// [y] => 3.445
// [label] => point #1
// )

?>

//**********************
get_parent_class (PHP4 >= 4.0b2)

string get_parent_class (object obj)

返回obj的类的父类的名称。

//***********************
is_subclass_of (PHP4 >= 4.0b4)

bool is_subclass_of (object obj, string superclass)

如果obj是superclass的类的一个子类的实例对象,返回true。

//*************************
method_exists (PHP4 >= 4.0b2)

bool method_exists (object object, string method_name)

如果method_name是object对象的一个方法,返回真。

//****************************


百度中 再介绍几个与类/对象相关的新函数 相关内容
Google搜索中 再介绍几个与类/对象相关的新函数 相关内容
技术文章快速查找

栏目导航
软件应用
·操作系统 ·杀毒防黑 ·应用软件
·聊天软件 ·网络软件  
Web开发
·ASP ·JavaScript ·DIV+CSS
·JSP ·VbScript ·Web服务器
·PHP ·XML  
开发语言
·VB ·VC ·ASP.NET
·Java ·C/C++ ·Delphi
数据库开发
·MySQL ·SQL/Access ·PowerBuilder
·Oracle ·DB2  
网站设计
·Flash ·Dreamweaver ·HTML/CSS
·Fireworks ·FrontPage  
平面设计
·Photoshop ·CorelDraw ·AutoCAD
·FreeHand ·Illustrator ·3DsMAX
网络技术
·路由器 ·交换机 ·服务器
·运维管理 ·无线网络 ·布线技术
·网络安全    

相关软件 相关文章
用PHP实现文件上传
php和xml--使用WDDX函数(1)
PHP4.0.1的变化(三)
介绍几个Array函数库中的新函数
怎样在PHP中通过ADO调用Access数据库和COM程序
基于PHP的聊天室(三)
PHP4.0.1的变化(二)
PHP & JavaScript控制系列:客户端数据存储(三)

相关软件 产品库推荐
·笔记本 ·平板电脑 ·上网本
·数码相机 ·手机 ·GPS
·DV摄像机 ·MP3 ·MP4
·CPU/硬盘/内存 ·音箱 ·主板
·键鼠套装 ·显卡 ·显示器
·打印机 ·投影机 ·路由器

Copyright 2011 www.zhujiangroad.com All Rights Reserved.
珠江路在线版权所有 苏ICP备05016148号
 
关于我们 |  广告服务 |  付款方式 |  南京地图 | 南京公交查询 | 南京火车时刻表 | 站长工具