当前位置:首页> PHP教程> php基础
关键字
文章内容
再介绍几个与类/对象相关的新函数
 
 
修改时间:[2010/09/28 20:38]    阅读次数:[862]    发表者:[起缘]
 
作者:sharetop
上回我介绍了几个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
// 基类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对象的一个方法,返回真。
//****************************
好了,就是上面这几个了。如果有什么问题,请与我联系,多谢。
ycshowtop@21cn.com
出处:oso.com.cn
phpfans.net收集整理