类_类的常用辅助函数

类的常用辅助函数

1. class_exists(); 2.get_class(); 3.get_methods(); 4.ger_parent_class;
5. is_a();6. interface_exists;7. is_subclass_of();8. is_method_exists();

<?php
/*
* 类辅助函数
*/
/****************************************************class_exists()函数****************************************/
echo "<hr>/***************************************class_exists()函数 ***************************/<br>";
class test{
}
if (class_exists(test)){
echo "当前脚本上下文中有该类存在! ";
}
else{
echo "当前脚本上下文中没有该类的存在!";
}

/****************************************************get_class()函数****************************************/
echo "<hr>/***************************************get_class函数 ************************************/<br>";
class myclass{
}
$app = new myclass();
if($classname = get_class($app));
echo "/$app对象属于/"$classname/"类";

/****************************************************get_class_methods()函数*******************************/
echo "<hr>/***************************************get_class_methods函数 ***************************/<br>";
class myclass2{
function m_first(){
   echo "This is the first method!";
}
function m_second(){
   echo "This is the second method!";
}
function m_third(){
   echo "This is the third method!";
}
}
$arr_method = get_class_methods(myclass2);
echo "Here are tho methods in :<br>";
foreach($arr_method as $key=>$value){
echo "$key=>$value"."<br>";
}

/*******************************************get_parent_class()函数****************************************/
echo "<hr>/***************************************get_parent_class函数 ***************************/<br>";
class father{
}
class son extends father{

}
$appson = new son;
echo "The father of the object '/$app' is ". get_parent_class($appson) ;
echo "<br>";
echo "The father of the class 'son' is ". get_parent_class($appson) ;

/*******************************************interface_exists()函数****************************************/
echo "<hr>/******************************interface_exists()函数 ************************************/<br>";
interface test_interface{
function test();
}
if(interface_exists(test_interface))
{ echo "The interface named 'test_interface' is exist!";}
else
{ echo "The interface named 'test_interface' isn't exist!";}

/****************************************************is_a()函数*************************************************/
echo "<hr>/***************************************is_a()函数 ************************************/<br>";
class class_hoho{
}
class class_hoho_son extends class_hoho{
}
$hoho = new class_hoho_son();
if(is_a($hoho,class_hoho_son))
{echo "对象/$hoho属于类class_hoho_son";}
else
{echo "对象/$hoho不属于类class_hoho_son";}
echo "<br>";
if(is_a($hoho,class_hoho))
{echo "对象/$hoho属于类class_hoho的子类";}
else
{{echo "对象/$hoho不属于类class_hoho的子类";}}

/*************************is_subclass_of()函数*******************************/
echo "<hr>/***************************************is_subclass_of()函数 ************************************/<br>";
class class_father{
}
class class_son extends class_father{
}
$apphoho = new class_son();
if(is_subclass_of($apphoho,class_father))
{echo "对象/$apphoho属于类class_father的子类所定义";}
else
   {echo "对象/$apphoho不属于类class_father的子类所定义";}

/*************************is_method_exists()函数*******************************/
echo "<hr>/***************************************is_method_exists()函数 ************************************/<br>";
class sumer{
public $start;
public $stop;

function __construct($start,$stop){
   $this->start=$start;
   $this->stop=$stop;
}
function sum(){
   for($sum=0,$i=$this->start;$i<=$this->stop;$i++){
    $sum=$sum+$i;
   }
   echo $sum;
}
}
$sum1 = new sumer('1','100');
if(method_exists(sumer,sum)){
echo "类sum中存在方法sumer<br>从1加到100的结果为:";
$sum1->sum();
}
else
{echo "类sum中不存在方法sumer";}
?>

运行结果:


/***************************************class_exists()函数 ***************************/
当前脚本上下文中有该类存在!


/***************************************get_class函数 ************************************/
$app对象属于"myclass"类


/***************************************get_class_methods函数 ***************************/
Here are tho methods in :
0=>m_first
1=>m_second
2=>m_third


/***************************************get_parent_class函数 ***************************/
The father of the object '$app' is father
The father of the class 'son' is father


/******************************interface_exists()函数 ************************************/
The interface named 'test_interface' is exist!


/***************************************is_a()函数 ************************************/
对象$hoho属于类class_hoho_son
对象$hoho属于类class_hoho的子类


/***************************************is_subclass_of()函数 ************************************/
对象$apphoho属于类class_father的子类所定义


/***************************************is_method_exists()函数 ************************************/
类sum中存在方法sumer
从1加到100的结果为:5050

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值