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 = 0 => $value = privateFunction

$key = 1 => $value = publicFunction

get_class_vars.....................

$key = publicVar => $value = 1

$key = privateVar => $value = 2

$key = staticPrivateVar => $value = hello

$key = staticPublicVar => $value =

get_object_vars.....................

$key = publicVar => $value = 1

$key = privateVar => $value = 2

The following is output out of TestClass.

get_class_methods.....................

$key = 0 => $value = publicFunction

get_class_vars.....................

$key = publicVar => $value = 1

$key = staticPublicVar => $value =

get_object_vars.....................

$key = publicVar => $value = 1

复制代码

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(4) "Base"

string(6) "Derive"

string(4) "Base"

string(6) "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)

复制代码

时间: 2024-08-26 01:31:38

PHP类和对象函数实例详解的相关文章

JAVA 类和对象基础知识详解

/*文章中用到的代码只是一部分,需要源码的可通过邮箱联系我 [email protected]*/ 和C++一样,JAVA也是一门面向对象的语言,其基础和核心是类和对象.而面向对象的思想是来源与显示生活,自然在学习时联系现实生活会理解得更深. 1.对象 首先,什么是对象? 在现实生活中只要是一个具体的物体或者一个具体的事物就是一个对象.你所看到的任何一个物体,你所想的任何一个物体都是对象,比如:某一台电脑,某一个人 其次,知道了什么是对象之后便是去描述.你会从哪些方面去描述对象?分别有些什么内容

Java4Android类和对象的初始化详解

1,成员初始化 Java尽力保证:所有变量在使用前都能够恰当的初始化. 1)方法的局部变量.Java以编译时错误来贯彻这种保证.eg: void f(){ int i; i++; //Error , i not initialized } 2)类的数据成员.如果是基本类型,他们都会有一个初始值:如果是对象引用,那么这个引用将会被初始化为null. 指定初始化 如果想为某个变量赋值,该怎么做? 1)直接在定义类成员变量的地方为其赋值(注意,C++里面是不可以的,尽管C++新手们总想这么做) cla

php中自动加载类_autoload()和spl_autoload_register()实例详解

一._autoload 自动加载类:当我们实例化一个未定义的类时,就会触此函数.到了php7.1以后版本不支持此函数好像抛弃了 新建一个类文件名字自己随便去:news类在auto.php文件里面去实例news类而没有引入该类,可以用_autoload自动加载方法类去处理. news.class.php文件 class news{ function do_new() { echo 'aaa'; } } auto.php文件使用_autoload函数要定义函数体自己去定义 function __au

Adapter类下的Gallery实例详解

通常我们更多的继承BaseAdapter来编写自己的Adapter类,因为BaseAdapter类是其他Adapter类的基类.在世界的运用过程中呢,我们一般需要重写这类的一些方法: getCount() ;获取当前Adapter的Items数目 getItem(int position);获取相应Position(位置)的Item getItemId(int position);获取相应position的Item在LIst中的row id getView(int positon,View co

PHP函数处理函数实例详解

1. call_user_func和call_user_func_array:  以上两个函数以不同的参数形式调用回调函数.见如下示例: <?php class AnotherTestClass { public static function printMe() { print "This is Test2::printSelf.\n"; } public function doSomething() { print "This is Test2::doSomethi

C# Oracle数据库操作类实例详解

本文所述为C#实现的Oracle数据库操作类,可执行超多常用的Oracle数据库操作,包含了基础数据库连接.关闭连接.输出记录集.执行Sql语句,返回带分页功能的dataset .取表里字段的类型和长度等,同时还有哈稀表自动插入数据库等高级任务.需要特别指出的是:在执行SQL语句,返回 DataReader之前一定要先用.read()打开,然后才能读到数据,再用hashTable对数据库进行insert,update,del操作,注意此时只能用默认的数据库连接"connstr". 本文

实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(一)

在数据库有外键的时候,使用 select_related() 和 prefetch_related() 可以很好的减少数据库请求的次数,从而提高性能.本文通过一个简单的例子详解这两个函数的作用.虽然QuerySet的文档中已经详细说明了,但本文试图从QuerySet触发的SQL语句来分析工作方式,从而进一步了解Django具体的运作方式. 本来打算写成一篇单独的文章的,但是写完select_related()之后发现长度已经有点长了,所以还是写成系列,大概在两到三篇.整个完成之后将会在这里添加上

实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(二)

这是本系列的第二篇,内容是 prefetch_related() 函数的用途.实现途径.以及使用方法. 本系列的第一篇在这里 3. prefetch_related() 对于多对多字段(ManyToManyField)和一对多字段,可以使用prefetch_related()来进行优化.或许你会说,没有一个叫OneToManyField的东西啊.实际上 ,ForeignKey就是一个多对一的字段,而被ForeignKey关联的字段就是一对多字段了. 作用和方法 prefetch_related(

PE文件结构与函数导出表——详解与实例

PE文件结构与函数导出表--详解与实例 随着windows系统从Xp升级到Win7.Win8, 从32位升级到64位,PE文件结构在整体未变的情况下发生了一些小的变动,一方面是推荐的程序装载地址未采用,另一方面,导出函数序号不再是简单的升序,而是一定程度上的进行了乱序.本文首先对PE文件结构进行了详尽的解说,接着介绍了如何得出函数导出表,整个过程采用SysWoW64目录下的wininet.dll实例进行说明.在介绍过程中,明确指出了Win7.Win8等新系统相对Xp带来的区别. 文章链接:htt