php专区

 首页 > php专区 > PHP高级 > 面向对象MVC > php interface_exists、class_exists、method_exists和propert

php interface_exists、class_exists、method_exists和propert

分享到:
【字体:
导读:
          下面我们一起来看在php 中PHP类和对象函数这 php interface_exists、class_exists、method_exists和property_exists详解,希望文章对各...

php interface_exists、class_exists、method_exists和property_exists介绍

下面我们一起来看在php 中PHP类和对象函数这 php interface_exists、class_exists、method_exists和property_exists详解,希望文章对各位同学会有所帮助。

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) 判断指定类或者对象中是否含有指定的成员变量。

实例代码如下:

  1. //in another_test_class.php 
  2. interface AnotherTestInterface { 
  3. class AnotherTestClass { 
  4.     public static function printMe() { 
  5.         print "This is Test2::printSelf.n"
  6.     } 
  7.     public function doSomething() { 
  8.         print "This is Test2::doSomething.n"
  9.     } 
  10.     public function doSomethingWithArgs($arg1$arg2) { 
  11.         print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").n"
  12.     } 
  13. //in class_exist_test.php, 下面测试代码中所需的类和接口位于another_test_class.php, 
  14. //由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。 
  15. //这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。 
  16. function __autoload($classname) { 
  17.     $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname)); 
  18.     require strtolower($nomilizedClassname).".php"
  19. //spl_autoload_register(function($classname) { 
  20. //    $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname)); 
  21. //    require strtolower($nomilizedClassname).".php"; 
  22. /
分享到:
PHP面向对象之旅:static变量与方法 - ph...
PHP面向对象之旅:static变量与方法 在php中static关键字用来修饰属性、方法,称这些属性、方法为静态属性、静态方法,下面我们一直来看看tatic变量与方法学习笔记。 static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或...
php中关于抽象(abstract)类和抽象方法...
php中关于抽象(abstract)类和抽象方法的问题解析 下面我们一起来看看php中关于抽象(abstract)类和抽象方法的问题解析,希望本文章对各位同学会有所帮助哦。 在面向对象(OOP)语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法作为外部代码访问的接口。而抽象方法就是...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……