Home > php教程 > php手册 > body text

PHP类和对象函数实例详解

WBOY
Release: 2016-06-14 00:02:32
Original
932 people have browsed it

1. interface_exists、class_exists、method_exists和property_exists:

      顾名思义,从以上几个函数的命名便可以猜出几分他们的功能。我想这也是我随着对PHP的深入学习而越来越喜欢这门编程语言的原因了吧。下面先给出他们的原型声明和简短说明,更多的还是直接看例子代码吧。
bool interface_exists (string $interface_name [, bool $autoload = true ]) 判断接口是否存在,第二个参数表示在查找时是否执行__autoload。
bool class_exists (string $class_name [, bool $autoload = true ]) 判断类是否存在,第二个参数表示在查找时是否执行__autoload。
bool method_exists (mixed $object , string $method_name) 判断指定类或者对象中是否含有指定的成员函数。
bool property_exists (mixed $class , string $property) 判断指定类或者对象中是否含有指定的成员变量。

<span style="color: #000000;">php
</span><strong><span style="color: #008000;">//</span></strong><span style="color: #008000;"><strong>in another_test_class.php</strong></span>
<span style="color: #0000ff;">interface</span><span style="color: #000000;"> AnotherTestInterface {

}

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> AnotherTestClass {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printMe() {
        </span><span style="color: #0000ff;">print</span> "This is Test2::printSelf.\n"<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doSomething() {
        </span><span style="color: #0000ff;">print</span> "This is Test2::doSomething.\n"<span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> doSomethingWithArgs(<span style="color: #800080;">$arg1</span>, <span style="color: #800080;">$arg2</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">print</span> 'This is Test2::doSomethingWithArgs with ($arg1 = '.<span style="color: #800080;">$arg1</span>.' and $arg2 = '.<span style="color: #800080;">$arg2</span>.").\n"<span style="color: #000000;">;
    }
}

</span><span style="color: #000000;">php
</span><strong><span style="color: #008000;">//</span></strong><span style="color: #008000;"><strong>in class_exist_test.php,</strong> 下面测试代码中所需的类和接口位于another_test_class.php,
//由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。
//这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。</span>
<span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$classname</span><span style="color: #000000;">) {
    </span><span style="color: #800080;">$nomilizedClassname</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #008080;">preg_replace</span>('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',<span style="color: #800080;">$classname</span><span style="color: #000000;">));
    </span><span style="color: #0000ff;">require</span> <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$nomilizedClassname</span>).".php"<span style="color: #000000;">;
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">spl_autoload_register(function($classname) {
//    $nomilizedClassname = strtolower(preg_replace('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',$classname));
//    require strtolower($nomilizedClassname).".php";
//});</span>

<span style="color: #0000ff;">print</span> "The following case is tested before executing autoload.\n"<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">class_exists</span>('AnotherTestClass',<span style="color: #0000ff;">false</span><span style="color: #000000;">)) {
    </span><span style="color: #0000ff;">print</span> "This class doesn't exist if no autoload.\n"<span style="color: #000000;">;
}

</span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">interface_exists</span>('AnotherTestInterface',<span style="color: #0000ff;">false</span><span style="color: #000000;">)) {
    </span><span style="color: #0000ff;">print</span> "This interface doesn't exist if no autoload.\n"<span style="color: #000000;">;
}

</span><span style="color: #0000ff;">print</span> "\nThe following case is tested after executing autoload.\n"<span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">class_exists</span>('AnotherTestClass',<span style="color: #0000ff;">true</span><span style="color: #000000;">)) {
    </span><span style="color: #0000ff;">print</span> "This class exists if autoload is set to true.\n"<span style="color: #000000;">;
}

</span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">interface_exists</span>('AnotherTestInterface',<span style="color: #0000ff;">true</span><span style="color: #000000;">)) {
    </span><span style="color: #0000ff;">print</span> "This interface exists if autoload is set to true.\n"<span style="color: #000000;">;
}</span> 
Copy after login

    运行结果如下: 

<span style="color: #000000;">bogon:TestPhp$ php class_exist_test.php 
The following </span><span style="color: #000000;">case</span><span style="color: #000000;"> is tested before executing autoload.
This class doesn</span><span style="color: #800000;">'</span><span style="color: #800000;">t exist if no autoload.</span>
This interface doesn<span style="color: #800000;">'</span><span style="color: #800000;">t exist if no autoload.</span>
<span style="color: #000000;">
The following </span><span style="color: #000000;">case</span><span style="color: #000000;"> is tested after executing autoload.
</span><span style="color: #000000;">This class exists if autoload is set to true.
This interface exists if autoload is set to true.</span>
Copy after login

2. get_declared_classes和get_declared_interfaces: 

    分别返回当前可以访问的所有类和接口,这不仅包括自定义类和接口,也包括了PHP内置类和接口。他们的函数声明非常简单,没有参数,只是返回数组。见如下代码:

<span style="color: #000000;">php
</span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> AnotherTestInterface {

}

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> AnotherTestClass {
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printMe() {
        </span><span style="color: #0000ff;">print</span> "This is Test2::printSelf.\n"<span style="color: #000000;">;
    }
}

</span><span style="color: #008080;">print_r</span>(<span style="color: #008080;">get_declared_interfaces</span><span style="color: #000000;">());
</span><span style="color: #008080;">print_r</span>(<span style="color: #008080;">get_declared_classes</span>());
Copy after login

    由于输出结果过长,而且这两个函数也比较简单,所以下面就不再给出输出结果了。

3. get_class_methods、get_class_vars和get_object_vars: 

    这三个函数有一个共同点,即只能获取作用域可见范围内的所有成员函数、成员变量或非静态成员变量。比如在类的内部调用,则所有成员函数或者变量都符合条件,而在类的外部,则只有共有的函数和变量可以返回。
array get_class_methods (mixed $class_name) 获取指定类中可访问的成员函数。
array get_class_vars (string $class_name) 获取指定类中可以访问的成员变量。
array get_object_vars (object $object) 获取可以访问的非静态成员变量。

<span style="color: #000000;">php
</span><span style="color: #0000ff;">function</span> output_array(<span style="color: #800080;">$functionName</span>, <span style="color: #800080;">$items</span><span style="color: #000000;">) {
    </span><span style="color: #0000ff;">print</span> "<span style="color: #800080;">$functionName</span>.....................\n"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$items</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">print</span> '$key = '.<span style="color: #800080;">$key</span>. ' => $value = '.<span style="color: #800080;">$value</span>."\n"<span style="color: #000000;">;
    }
}

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> TestClass {
    </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$publicVar</span> = 1<span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$privateVar</span> = 2<span style="color: #000000;">;
    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$staticPrivateVar</span> = "hello"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$staticPublicVar</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> privateFunction() {

    }
    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> publicFunction() {
        output_array(</span>"get_class_methods",<span style="color: #008080;">get_class_methods</span>(<span style="color: #ff00ff;">__CLASS__</span><span style="color: #000000;">));
        output_array(</span>'get_class_vars',<span style="color: #008080;">get_class_vars</span>(<span style="color: #ff00ff;">__CLASS__</span><span style="color: #000000;">));
        output_array(</span>'get_object_vars',<span style="color: #008080;">get_object_vars</span>(<span style="color: #800080;">$this</span><span style="color: #000000;">));
    }
}

</span><span style="color: #800080;">$testObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TestClass();
</span><span style="color: #0000ff;">print</span> "The following is output within TestClass.\n"<span style="color: #000000;">;
</span><span style="color: #800080;">$testObj</span>-><span style="color: #000000;">publicFunction();

</span><span style="color: #0000ff;">print</span> "\nThe following is output out of TestClass.\n"<span style="color: #000000;">;
output_array(</span>'get_class_methods',<span style="color: #008080;">get_class_methods</span>('TestClass'<span style="color: #000000;">));
output_array(</span>'get_class_vars',<span style="color: #008080;">get_class_vars</span>('TestClass'<span style="color: #000000;">));
output_array(</span>'get_object_vars',<span style="color: #008080;">get_object_vars</span>(<span style="color: #800080;">$testObj</span>));
Copy after login

    运行结果如下:

<span style="color: #000000;">bogon:TestPhp liulei$ php class_exist_test.php 
The following is output within TestClass.
get_class_methods.....................
$key </span>= <span style="color: #800080;">0</span> => $value =<span style="color: #000000;"> privateFunction
$key </span>= <span style="color: #800080;">1</span> => $value =<span style="color: #000000;"> publicFunction
get_class_vars.....................
$key </span>= publicVar => $value = <span style="color: #800080;">1</span><span style="color: #000000;">
$key </span>= privateVar => $value = <span style="color: #800080;">2</span><span style="color: #000000;">
$key </span>= staticPrivateVar => $value =<span style="color: #000000;"> hello
$key </span>= staticPublicVar => $value =<span style="color: #000000;"> 
get_object_vars.....................
$key </span>= publicVar => $value = <span style="color: #800080;">1</span><span style="color: #000000;">
$key </span>= privateVar => $value = <span style="color: #800080;">2</span><span style="color: #000000;">

The following is output out of TestClass.
get_class_methods.....................
$key </span>= <span style="color: #800080;">0</span> => $value =<span style="color: #000000;"> publicFunction
get_class_vars.....................
$key </span>= publicVar => $value = <span style="color: #800080;">1</span><span style="color: #000000;">
$key </span>= staticPublicVar => $value =<span style="color: #000000;"> 
get_object_vars.....................
$key </span>= publicVar => $value = <span style="color: #800080;">1</span>
Copy after login

4. get_called_class和get_class:

string get_class ([ object $object = NULL ]) 获取参数对象的类名称。
string get_called_class (void) 静态方法调用时当前的类名称。

<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Base {
    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() {
        </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(get_called_class());
    }
}

</span><span style="color: #0000ff;">class</span> Derive <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base {
}

Base</span>::<span style="color: #000000;">test();
Derive</span>::<span style="color: #000000;">test();

</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_class</span>(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Base()));
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_class</span>(<span style="color: #0000ff;">new</span> Derive()));
Copy after login

    运行结果如下:

<span style="color: #000000;">bogon:TestPhp$ php another_test_class.php 
</span><span style="color: #0000ff;">string</span>(<span style="color: #800080;">4</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Base</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">string</span>(<span style="color: #800080;">6</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Derive</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">string</span>(<span style="color: #800080;">4</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Base</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">string</span>(<span style="color: #800080;">6</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Derive</span><span style="color: #800000;">"</span>
Copy after login

5. get_parent_class、is_a和is_subclass_of:

    这三个函数都是和类的继承相关,所以我把他们归到了一起。

string get_parent_class ([ mixed $object ]) 获取参数对象的父类,如果没有父类则返回false。
bool is_a (object $object, string $class_name) 判断第一个参数对象是否是$class_name类本身或是其父类的对象。
bool is_subclass_of (mixed $object, string $class_name) 判断第一个参数对象是否是$class_name的子类。

<span style="color: #000000;">php
</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Base {
    </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() {
        </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(get_called_class());
    }
}

</span><span style="color: #0000ff;">class</span> Derive <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base {
}

</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_parent_class</span>(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Derive()));
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_a</span>(<span style="color: #0000ff;">new</span> Derive(),'Derive'<span style="color: #000000;">));
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_a</span>(<span style="color: #0000ff;">new</span> Derive(),'Base'<span style="color: #000000;">));
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_a</span>(<span style="color: #0000ff;">new</span> Base(),'Derive'<span style="color: #000000;">));

</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_subclass_of</span>(<span style="color: #0000ff;">new</span> Derive(),'Derive'<span style="color: #000000;">));
</span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_subclass_of</span>(<span style="color: #0000ff;">new</span> Derive(),'Base'));
Copy after login

    运行结果如下:

<span style="color: #000000;">bogon:TestPhp$ php another_test_class.php 
</span><span style="color: #000000;">string(4) "Base"
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)</span>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!