php中类的使用。。。。介绍两个php类的函数。。。。
=========================================================================
copycode- <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>
|
copycode- <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>
|