罗江游鱼
级别: 论坛版主
UID: 1234
精华: 0
发帖: 79
威望: 80 点
金钱: 790 RMB
贡献值: 0 点
注册时间:2007-11-08
最后登录:2008-04-08
楼主  发表于: 2007-12-18 19:18

 is_subclass_of()、method_exists()

php中类的使用。。。。介绍两个php类的函数。。。。
=========================================================================
copycode
  1. <html>
  2. <head>
  3. <title>is_subclass_of()函数使用实例</title>
  4. </head>
  5. <body>
  6. <?
  7. class Window                                        //首先定义一个类
  8. {
  9.     var $state;                                    //窗户的状态
  10.     function close_window()                            //关窗户方法
  11.     {
  12.         $this->state="close";                            //窗户的状态为关
  13.     }
  14.     function open_window()                            //开窗户方法
  15.     {
  16.         $this->state="open";                            //窗户的状态为开
  17.     }
  18. }
  19. class Who_Window extends Window                    //创建Window的子类Who_Window
  20. {
  21.     var $owner;
  22.     function close_window()                            //方法继承
  23.     {
  24.         $this->state="close";
  25.         $this->owner="Jack";
  26.     }
  27. }
  28. function is_sub_e($obj,$string)                            //创建一个基于is_subclass_of()的自定义函数
  29. {
  30.     if(is_subclass_of($obj,$string))                        //如果类名存在
  31.     {
  32.         echo "对象属于名为".$string."的类的子类的对象!";//打印相应信息
  33.     }
  34.     else                                            //如果类不存在
  35.     {
  36.         echo "对象不属于名为".$string."的类的子类的对象!";//打印相应信息
  37.     }
  38. }
  39. class Dog                                            //首先定义一个类
  40. {
  41.     var $name;                                    //狗的名字
  42.     var $age;                                        //狗的年龄
  43.     var $birthday;                                    //狗的生日
  44.     var $sex;                                        //狗的性别
  45. }
  46. $my_window=new Who_Window;
  47. is_sub_e($my_window,"Who_Window");                    //调用自定义函数
  48. echo "<p>";
  49. is_sub_e($my_window,"Window");                        //调用自定义函数
  50. echo "<p>";
  51. is_sub_e($my_window,"Dog");                            //调用自定义函数
  52. ?>
  53. </body>
  54. </html>


copycode
  1. <html>
  2. <head>
  3. <title>method_exists()函数使用实例</title>
  4. </head>
  5. <body>
  6. <?
  7. class Window                                        //首先定义一个类
  8. {
  9.     var $state;                                    //窗户的状态
  10.     function close_window()                            //关窗户方法
  11.     {
  12.         $this->state="close";                            //窗户的状态为关
  13.     }
  14.     function open_window()                            //开窗户方法
  15.     {
  16.         $this->state="open";                            //窗户的状态为开
  17.     }
  18. }
  19. function method_e($obj,$string)                        //创建一个基于method_exists的自定义函数
  20. {
  21.     if(method_exists($obj,$string))                        //如果类名存在
  22.     {
  23.         echo "对象中名为".$string."的类已经存在!";        //打印相应信息
  24.     }
  25.     else                                            //如果类不存在
  26.     {
  27.         echo "对象中名为".$string."的类并不存在!";        //打印相应信息
  28.     }
  29. }
  30. $my_window=new Window;
  31. method_e($my_window,"open_window");                    //调用自定义函数
  32. echo "<p>";
  33. method_e($my_window,"close_window");                    //调用自定义函数
  34. echo "<p>";
  35. method_e($my_window,"temp_method");                    //调用自定义函数
  36. ?>
  37. </body>
  38. </html>
lxydyx
级别: 新手上路
UID: 2193
精华: 0
发帖: 1
威望: 2 点
金钱: 10 RMB
贡献值: 0 点
注册时间:2008-02-20
最后登录:2008-04-02
1楼  发表于: 2008-04-02 13:52
搞不懂干什么的
jacking
级别: 论坛版主
UID: 2
精华: 2
发帖: 295
威望: 309 点
金钱: 2990 RMB
贡献值: 0 点
注册时间:2007-05-17
最后登录:2008-12-31
2楼  发表于: 2008-04-04 17:17
这是一个介绍类使用的实际例子而已……