本页主题: is_subclass_of()、method_exists() 打印 | 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题

罗江游鱼

该用户目前不在线
级别: 论坛版主
精华: 0
发帖: 79
威望: 80 点
金钱: 790 RMB
贡献值: 0 点
注册时间:2007-11-08
最后登录:2008-04-08

is_subclass_of()、method_exists()

php中类的使用。。。。介绍两个php类的函数。。。。
=========================================================================
Copy code
<html>
<head>
<title>is_subclass_of()函数使用实例</title>
</head>
<body>
<?
class Window                                        //首先定义一个类
{
    var $state;                                    //窗户的状态
    function close_window()                            //关窗户方法
    {
        $this->state="close";                            //窗户的状态为关
    }
    function open_window()                            //开窗户方法
    {
        $this->state="open";                            //窗户的状态为开
    }
}
class Who_Window extends Window                    //创建Window的子类Who_Window
{
    var $owner;
    function close_window()                            //方法继承
    {
        $this->state="close";
        $this->owner="Jack";
    }
}
function is_sub_e($obj,$string)                            //创建一个基于is_subclass_of()的自定义函数
{
    if(is_subclass_of($obj,$string))                        //如果类名存在
    {
        echo "对象属于名为".$string."的类的子类的对象!";//打印相应信息
    }
    else                                            //如果类不存在
    {
        echo "对象不属于名为".$string."的类的子类的对象!";//打印相应信息
    }
}
class Dog                                            //首先定义一个类
{
    var $name;                                    //狗的名字
    var $age;                                        //狗的年龄
    var $birthday;                                    //狗的生日
    var $sex;                                        //狗的性别
}
$my_window=new Who_Window;
is_sub_e($my_window,"Who_Window");                    //调用自定义函数
echo "<p>";
is_sub_e($my_window,"Window");                        //调用自定义函数
echo "<p>";
is_sub_e($my_window,"Dog");                            //调用自定义函数
?>
</body>
</html>


Copy code
<html>
<head>
<title>method_exists()函数使用实例</title>
</head>
<body>
<?
class Window                                        //首先定义一个类
{
    var $state;                                    //窗户的状态
    function close_window()                            //关窗户方法
    {
        $this->state="close";                            //窗户的状态为关
    }
    function open_window()                            //开窗户方法
    {
        $this->state="open";                            //窗户的状态为开
    }
}
function method_e($obj,$string)                        //创建一个基于method_exists的自定义函数
{
    if(method_exists($obj,$string))                        //如果类名存在
    {
        echo "对象中名为".$string."的类已经存在!";        //打印相应信息
    }
    else                                            //如果类不存在
    {
        echo "对象中名为".$string."的类并不存在!";        //打印相应信息
    }
}
$my_window=new Window;
method_e($my_window,"open_window");                    //调用自定义函数
echo "<p>";
method_e($my_window,"close_window");                    //调用自定义函数
echo "<p>";
method_e($my_window,"temp_method");                    //调用自定义函数
?>
</body>
</html>
顶端 Posted: 2007-12-18 19:18 | [楼 主]
lxydyx

该用户目前不在线
级别: 新手上路
精华: 0
发帖: 1
威望: 2 点
金钱: 10 RMB
贡献值: 0 点
注册时间:2008-02-20
最后登录:2008-04-02

搞不懂干什么的
顶端 Posted: 2008-04-02 13:52 | 1 楼
jacking

该用户目前不在线
级别: 论坛版主
精华: 2
发帖: 294
威望: 299 点
金钱: 2980 RMB
贡献值: 0 点
注册时间:2007-05-17
最后登录:2008-08-07

这是一个介绍类使用的实际例子而已……
顶端 Posted: 2008-04-04 17:17 | 2 楼
开源嘛论坛 » php示例

Guest cache page, Update at 2008-12-02 06:28 桂ICP备07006725号
Powered by PHPWind v6.0 Certificate Code © 2003-07 PHPWind.com Corporation