PHP类和对象函数实例详解
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) 判断指定类或者对象中是否含有指定的成员变量。
<?php
//in another_test_class.php
interface AnotherTestInterface { } class AnotherTestClass {
public static function printMe() {
print "This is Test2::printSelf.\n";
}
public function doSomething() {
print "This is Test2::doSomething.\n";
}
public function doSomethingWithArgs($arg1, $arg2) {
print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").\n";
}
} <?php
//in class_exist_test.php, 下面测试代码中所需的类和接口位于another_test_class.php,
//由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。
//这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。
function __autoload($classname) {
$nomilizedClassname = strtolower(preg_replace('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',$classname));
require strtolower($nomilizedClassname).".php";
}
//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";
//}); print "The following case is tested before executing autoload.\n";
if (!class_exists('AnotherTestClass',false)) {
print "This class doesn't exist if no autoload.\n";
} if (!interface_exists('AnotherTestInterface',false)) {
print "This interface doesn't exist if no autoload.\n";
} print "\nThe following case is tested after executing autoload.\n";
if (class_exists('AnotherTestClass',true)) {
print "This class exists if autoload is set to true.\n";
} if (interface_exists('AnotherTestInterface',true)) {
print "This interface exists if autoload is set to true.\n";
}
运行结果如下:
bogon:TestPhp$ php class_exist_test.php
The following case is tested before executing autoload.
This class doesn't exist if no autoload.
This interface doesn't exist if no autoload. The following case is tested after executing autoload.
This class exists if autoload is set to true.
This interface exists if autoload is set to true.
2. get_declared_classes和get_declared_interfaces:
分别返回当前可以访问的所有类和接口,这不仅包括自定义类和接口,也包括了PHP内置类和接口。他们的函数声明非常简单,没有参数,只是返回数组。见如下代码:
<?php
interface AnotherTestInterface { } class AnotherTestClass {
public static function printMe() {
print "This is Test2::printSelf.\n";
}
} print_r(get_declared_interfaces());
print_r(get_declared_classes());
由于输出结果过长,而且这两个函数也比较简单,所以下面就不再给出输出结果了。
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) 获取可以访问的非静态成员变量。
<?php
function output_array($functionName, $items) {
print "$functionName.....................\n";
foreach ($items as $key => $value) {
print '$key = '.$key. ' => $value = '.$value."\n";
}
} class TestClass {
public $publicVar = 1;
private $privateVar = 2;
static private $staticPrivateVar = "hello";
static public $staticPublicVar; private function privateFunction() { }
function publicFunction() {
output_array("get_class_methods",get_class_methods(__CLASS__));
output_array('get_class_vars',get_class_vars(__CLASS__));
output_array('get_object_vars',get_object_vars($this));
}
} $testObj = new TestClass();
print "The following is output within TestClass.\n";
$testObj->publicFunction(); print "\nThe following is output out of TestClass.\n";
output_array('get_class_methods',get_class_methods('TestClass'));
output_array('get_class_vars',get_class_vars('TestClass'));
output_array('get_object_vars',get_object_vars($testObj));
运行结果如下:
bogon:TestPhp liulei$ php class_exist_test.php
The following is output within TestClass.
get_class_methods.....................
$key = => $value = privateFunction
$key = => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value =
$key = privateVar => $value =
$key = staticPrivateVar => $value = hello
$key = staticPublicVar => $value =
get_object_vars.....................
$key = publicVar => $value =
$key = privateVar => $value = The following is output out of TestClass.
get_class_methods.....................
$key = => $value = publicFunction
get_class_vars.....................
$key = publicVar => $value =
$key = staticPublicVar => $value =
get_object_vars.....................
$key = publicVar => $value =
4. get_called_class和get_class:
string get_class ([ object $object = NULL ]) 获取参数对象的类名称。
string get_called_class (void) 静态方法调用时当前的类名称。
<?php
class Base {
static public function test() {
var_dump(get_called_class());
}
} class Derive extends Base {
} Base::test();
Derive::test(); var_dump(get_class(new Base()));
var_dump(get_class(new Derive()));
运行结果如下:
bogon:TestPhp$ php another_test_class.php
string() "Base"
string() "Derive"
string() "Base"
string() "Derive"
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的子类。
<?php
class Base {
static public function test() {
var_dump(get_called_class());
}
} class Derive extends Base {
} var_dump(get_parent_class(new Derive()));
var_dump(is_a(new Derive(),'Derive'));
var_dump(is_a(new Derive(),'Base'));
var_dump(is_a(new Base(),'Derive')); var_dump(is_subclass_of(new Derive(),'Derive'));
var_dump(is_subclass_of(new Derive(),'Base'));
运行结果如下:
bogon:TestPhp$ php another_test_class.php
string(4) "Base"
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
PHP类和对象函数实例详解的更多相关文章
- [Java]Java类和对象内存分配详解
描述 代码说明: 一.当Person p1 = new Person();第一次被调用时需要做两件事: 1.先判断类加载器是否加载过Person类,如果没有则加载到Person类型到方法区 2.在堆中 ...
- 2.java面向对象类与类/类与对象之间关系详解
继承.实现.依赖.关联.聚合.组合的联系与区别 下面的内容很基础,同时也很简单,但是也很重要. 继承 指的是一个类(称为子类.子接口)继承另外的一个类(称为父类.父接口)的功能,并可以增加它自己的新功 ...
- JAVA 类和对象基础知识详解
/*文章中用到的代码只是一部分,需要源码的可通过邮箱联系我 1978702969@qq.com*/ 和C++一样,JAVA也是一门面向对象的语言,其基础和核心是类和对象.而面向对象的思想是来源与显示生 ...
- C++ string 类的 find 方法实例详解
1.C++ 中 string 类的 find 方法列表 size_type std::basic_string::find(const basic_string &__str, size_ty ...
- php中自动加载类_autoload()和spl_autoload_register()实例详解
一._autoload 自动加载类:当我们实例化一个未定义的类时,就会触此函数.到了php7.1以后版本不支持此函数好像抛弃了 新建一个类文件名字自己随便去:news类在auto.php文件里面去实例 ...
- Qt QString类及常用函数功能详解
QString 是 Qt 编程中常用的类,除了用作数字量的输入输出之外,QString 还有很多其他功能,熟悉这些常见的功能,有助于灵活地实现字符串处理功能. QString 存储字符串釆用的是 Un ...
- PHP函数处理函数实例详解
1. call_user_func和call_user_func_array: 以上两个函数以不同的参数形式调用回调函数.见如下示例: <?php class AnotherTestClass ...
- php 如何禁用eval() 函数实例详解
在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险并经常会出现一些问题,今天我们就一起来看看eval函数对数组的操作及php 如何禁用eval() 函数: <?php $ ...
- Java中JSON字符串与java对象的互换实例详解
这篇文章主要介绍了在java中,JSON字符串与java对象的相互转换实例详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JS ...
随机推荐
- Replication的犄角旮旯(七)-- 一个DDL引发的血案(下)(聊聊logreader的延迟)
<Replication的犄角旮旯>系列导读 Replication的犄角旮旯(一)--变更订阅端表名的应用场景 Replication的犄角旮旯(二)--寻找订阅端丢失的记录 Repli ...
- 快速学习C语言二: 编译自动化, 静态分析, 单元测试,coredump调试,性能剖析
上次的Hello world算是入门了,现在学习一些相关工具的使用 编译自动化 写好程序,首先要编译,就用gcc就好了,基本用法如下 gcc helloworld.c -o helloworld.o ...
- Android 事件拦截机制一种粗鄙的解释
对于Android事件拦截机制,相信对于大多数Android初学者是一个抓耳挠腮难于理解的问题.其实理解这个问题并不困难. 首先,你的明白事件拦截机制到底是怎么一回事?这里说的事件拦截机制,指的是对触 ...
- 自动升级系统OAUS的设计与实现(续) (附最新源码)
(最新OAUS版本请参见:自动升级系统的设计与实现(续2) -- 增加断点续传功能) 一.缘起 自从 自动升级系统的设计与实现(源码) 发布以后,收到了很多使用者的反馈,其中最多的要求就是希望OAUS ...
- MySQL7:视图
什么是视图 数据库中的视图是一个虚拟表.视图是从一个或者多个表中导出的表,视图的行为与表非常相似,在视图中用户可以使用SELECT语句查询数据,以及使用INSERT.UPDATE和DELETE修改记录 ...
- 《C#图解教程》读书笔记之五:委托和事件
本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.委托初窥:一个拥有方法的对象 (1)本质:持有一个或多个方法的对象:委托和典型的对象不同,执行委托实际上是执 ...
- Aspectj 实现Method条件运行
最近我花了半个小时实现了一个Method的按自定义条件运行的plugin,Condition-Run.实现场景是由于我所工作的客户经常会是在同一个代码集上实现多个Brand,所以有些功能只会限制是几个 ...
- 在.net中使用aquiles访问Cassandra(二)
上文中我们已经建立了项目的基本结构,今天实现数据的修改.在NoSQL中,通常添加和修改都认为是对数据的一种Mutation. 1.建立描述修改Row的实体. public class RowMut ...
- AWS助理架构师样题解析
AWS 认证是对其在 AWS 平台上设计.部署和管理应用程序所需的技能和技术知识的一种认可.获得证书有助于证明您使用 AWS 的丰富经验和可信度,同时还能提升您所在的组织熟练使用基于 AWS 云服务应 ...
- Git学习笔记(2)——版本的回退,和暂存区的理解
本文主要记录了版本的回退,以及工作区,暂存区概念的理解. //开始之前,先回顾上次的内容,修改文件如下,并提交到版本库. Git is a distributed version control sy ...