前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php interface exists,php interface_exists、class_exists、method_exists和propert

php interface exists,php interface_exists、class_exists、method_exists和propert

作者头像
全栈程序员站长
发布2022-09-14 13:25:03
4980
发布2022-09-14 13:25:03
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

下面我们一起来看在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) 判断指定类或者对象中是否含有指定的成员变量。

实例代码如下:

//in another_test_class.php

interfaceAnotherTestInterface {

}

classAnotherTestClass {

publicstaticfunctionprintMe() {

print “This is Test2::printSelf.n”;

}

publicfunctiondoSomething() {

print “This is Test2::doSomething.n”;

}

publicfunctiondoSomethingWithArgs($arg1,$arg2) {

print ‘This is Test2::doSomethingWithArgs with ($arg1 = ‘.$arg1.’ and $arg2 = ‘.$arg2.”).n”;

}

}

//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));

requirestrtolower($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”;

/

(责任编辑:admin)

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159652.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档