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

 class_exists() 与get_class_methods()函数使用实例



=========================================================================
copycode
  1. [blockquote]<html>
  2. <head>
  3. <title>class_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. class Who_Window extends Window  //创建子类
  20. {
  21. var $owner;
  22. function close_window()    //方法继承
  23. {
  24.   $this->state="close";
  25.   $this->owner="Jack";
  26. }
  27. }
  28. function f_e($string)      //创建一个基于class_exists()的自定义函数
  29. {
  30. if(class_exists($string))    //如果类名存在
  31. {
  32.   echo "名为".$string."的类已经存在!"; //打印相应信息
  33. }
  34. else        //如果类不存在
  35. {
  36.   echo "名为".$string."的类并不存在!"; //打印相应信息
  37. }
  38. }
  39. f_e("Window");        //调用函数
  40. echo "<p>";
  41. f_e("Who_Window");      //调用函数
  42. echo "<p>";
  43. f_e("temp_class");      //调用函数
  44. echo "<p>";
  45. ?>
  46. </body>
  47. </html>[/blockquote]


copycode
  1. [blockquote]<html>
  2. <head>
  3. <title>get_class_methods()函数使用实例</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. $temp=get_class_methods("Window");
  20. echo "类Window中的方法有以下几个:";
  21. echo "<p>";
  22. for($i=0;$i<count($temp);$i++)
  23. {
  24.     echo $temp[$i].",";
  25. }
  26. ?>
  27. </body>
  28. </html>[/blockquote]