Ref/spl-Phpdoc专题
class_implements
返回指定的类实现的所有接口。
说明
array <span
class="methodname">class_implements ( <span
class="methodparam">mixed $class [,
bool
$autoload ] )
本函数返回一个数组,该数组中包含了指定类class及其父类所实现的所有接口的名称。
参数
class
对象(类实例)或字符串(类名称)。
autoload
是否允许使用__autoload
魔术函数来自动装载该类。默认值为true。
返回值
调用成功则返回一个数组,否则返回false。
更新日志
| 版本 | 说明 |
|---|---|
| 5.1.0 | 增加了允许参数class为字符串的选项。增加了autoload参数。 |
范例
示例 #1 class_implements example
<?php
interface foo { }
class bar implements foo {}
print_r(class_implements(new bar));
// since PHP 5.1.0 you may also specify the parameter as a string
print_r(class_implements('bar'));
function __autoload($class_name) {
require_once $class_name . '.php';
}
// use __autoload to load the 'not_loaded' class
print_r(class_implements('not_loaded', true));
?>
以上例程的输出类似于:
Array
(
[foo] => foo
)
Array
(
[interface_of_not_loaded] => interface_of_not_loaded
)
参见
- class_parents
- get_declared_interfaces
class_parents
返回指定类的父类。
说明
array <span
class="methodname">class_parents ( <span
class="methodparam">mixed $class [,
bool
$autoload ] )
本函数返回一个包含了指定类class父类名称的数组。
参数
class
对象(类实例)或字符串(类名称)。
autoload
是否允许使用__autoload
魔术函数来自动装载该类。默认值为true。
返回值
调用成功则返回一个数组,否则返回false。
更新日志
| 版本 | 说明 |
|---|---|
| 5.1.0 | 增加了允许参数class为字符串的选项。增加了autoload参数。 |
范例
示例 #1 class_parents example
<?php
class foo { }
class bar extends foo {}
print_r(class_parents(new bar));
// since PHP 5.1.0 you may also specify the parameter as a string
print_r(class_parents('bar'));
function __autoload($class_name) {
require_once $class_name . '.php';
}
// use __autoload to load the 'not_loaded' class
print_r(class_parents('not_loaded', true));
?>
以上例程的输出类似于:
Array
(
[foo] => foo
)
Array
(
[parent_of_not_loaded] => parent_of_not_loaded
)
参见
- class_implements
class_uses
Return the traits used by the given class
说明
array <span
class="methodname">class_uses ( <span
class="type">mixed $class [, <span
class="methodparam">bool $autoload<span
class="initializer"> = true ] )
This function returns an array with the names of the traits that the
given class uses. This does however not include any traits used by a
parent class.
参数
class
An object (class instance) or a string (class name).
autoload
Whether to allow this function to load the class automatically through
the __autoload magic method.
返回值
An array on success, or false on error.
范例
示例 #1 class_uses example
<?php
trait foo { }
class bar {
use foo;
}
print_r(class_uses(new bar));
print_r(class_uses('bar'));
function __autoload($class_name) {
require_once $class_name . '.php';
}
// use __autoload to load the 'not_loaded' class
print_r(class_uses('not_loaded', true));
?>
以上例程的输出类似于:
Array
(
[foo] => foo
)
Array
(
[foo] => foo
)
Array
(
[trait_of_not_loaded] => trait_of_not_loaded
)
参见
- class_parents
- get_declared_traits
iterator_apply
为迭代器中每个元素调用一个用户自定义函数
说明
int <span
class="methodname">iterator_apply ( <span
class="methodparam">Traversable
$iterator , <span
class="type">callable $function [, <span
class="methodparam">array $args ] )
循环迭代每个元素时调用某一回调函数。
参数
iterator
需要循环迭代的类对象。
function
迭代到每个元素时的调用的回调函数。
Note: 为了遍历
iterator这个函数必须返回true。
args
传递到回调函数的参数。
返回值
返回已迭代的元素个数。
范例
示例 #1 iterator_apply example
<?php
function print_caps(Iterator $iterator) {
echo strtoupper($iterator->current()) . "\n";
return TRUE;
}
$it = new ArrayIterator(array("Apples", "Bananas", "Cherries"));
iterator_apply($it, "print_caps", array($it));
?>
以上例程会输出:
APPLES
BANANAS
CHERRIES
参见
- array_walk
iterator_count
计算迭代器中元素的个数
说明
int <span
class="methodname">iterator_count ( <span
class="methodparam">Traversable
$iterator )
计算迭代器中的元素个数。
参数
iterator
要计数的迭代器。
返回值
迭代器iterator中的元素个数。
范例
示例 #1 iterator_count example
<?php
$iterator = new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour'));
var_dump(iterator_count($iterator));
?>
以上例程会输出:
int(4)
iterator_to_array
将迭代器中的元素拷贝到数组
说明
array <span
class="methodname">iterator_to_array ( <span
class="methodparam">Traversable
$iterator [, <span
class="type">bool $use_keys =
true ] )
将迭代器中的元素拷贝到数组。
参数
iterator
被拷贝的迭代器。
use_keys
是否使用迭代器元素键作为索引。
返回值
一个数组,包含迭代器中的元素。
更新日志
| 版本 | 说明 |
|---|---|
| 5.2.1 | 添加了 use_keys 参数。 |
范例
示例 #1 iterator_to_array example
<?php
$iterator = new ArrayIterator(array('recipe'=>'pancakes', 'egg', 'milk', 'flour'));
var_dump(iterator_to_array($iterator, true));
var_dump(iterator_to_array($iterator, false));
?>
以上例程会输出:
array(4) {
["recipe"]=>
string(8) "pancakes"
[0]=>
string(3) "egg"
[1]=>
string(4) "milk"
[2]=>
string(5) "flour"
}
array(4) {
[0]=>
string(8) "pancakes"
[1]=>
string(3) "egg"
[2]=>
string(4) "milk"
[3]=>
string(5) "flour"
}
spl_autoload_call
尝试调用所有已注册的 __autoload() 函数来装载请求类
说明
void <span
class="methodname">spl_autoload_call ( <span
class="methodparam">string
$class_name )
可以直接在程序中手动调用此函数来使用所有已注册的 __autoload 函数装载类或接口。
参数
class_name
搜索的类名。
返回值
没有返回值。
spl_autoload_extensions
注册并返回 spl_autoload 函数使用的默认文件扩展名
说明
string <span
class="methodname">spl_autoload_extensions ([ <span
class="methodparam">string
$file_extensions ] )
本函数用来修改和检查 __autoload 函数内置的默认实现函数 spl_autoload 所使用的扩展名。
Note: 在定义的文件扩展名之间不应该有空格。
参数
file_extensions
当不使用任何参数调用此函数时,它返回当前的文件扩展名的列表,不同的扩展名用逗号分隔。要修改文件扩展名列表,用一个逗号分隔的新的扩展名列表字符串来调用本函数即可。中文注:默认的
spl_autoload 函数使用的扩展名是 ".inc,.php"。
返回值
逗号分隔的 spl_autoload 函数的默认文件扩展名。
范例
示例 #1 spl_autoload_extensions 示例
<?php
spl_autoload_extensions(".php,.inc");
?>
spl_autoload_functions
返回所有已注册的 __autoload() 函数
说明
array <span class="methodname">spl_autoload_functions ( <span class="methodparam">void )
获取所有已注册的 __autoload() 函数。
参数
此函数没有参数。
返回值
包含所有已注册的 __autoload 函数的数组(<span
class="type">array)。如果自动装载函数队列未激活,则返回
false。如果没有已注册的函数,则返回一个空数组。
spl_autoload_register
注册给定的函数作为 __autoload 的实现
说明
bool <span
class="methodname">spl_autoload_register ([ <span
class="methodparam">callable
$autoload_function [, <span
class="type">bool $throw =
true [, <span
class="type">bool $prepend =
false ]]] )
将函数注册到SPL __autoload函数队列中。如果该队列中的函数尚未激活,则激活它们。
如果在你的程序中已经实现了<span class="function">__autoload函数,它必须显式注册到<span class="function">__autoload队列中。因为 <span class="function">spl_autoload_register函数会将Zend Engine中的__autoload函数取代为<span class="function">spl_autoload或<span class="function">spl_autoload_call。
如果需要多条 autoload 函数,<span class="function">spl_autoload_register 满足了此类需求。 它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下, __autoload 只可以定义一次。
参数
autoload_function
欲注册的自动装载函数。如果没有提供任何参数,则自动注册 autoload
的默认实现函数spl_autoload。
throw
此参数设置了 autoload_function 无法成功注册时, <span
class="function">spl_autoload_register是否抛出异常。
prepend
如果是 true,spl_autoload_register
会添加函数到队列之首,而不是队列尾部。
返回值
成功时返回 true, 或者在失败时返回 false。
更新日志
| 版本 | 说明 |
|---|---|
| 5.3.0 | 引入了命名空间的支持。 |
| 5.3.0 | 添加了 prepend 参数。 |
范例
示例 #1 spl_autoload_register 作为 __autoload 函数的替代
<?php
// function __autoload($class) {
// include 'classes/' . $class . '.class.php';
// }
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
// 或者,自 PHP 5.3.0 起可以使用一个匿名函数
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});
?>
示例 #2 class 未能加载的 <span class="function">spl_autoload_register 例子
<?php
namespace Foobar;
class Foo {
static public function test($name) {
print '[['. $name .']]';
}
}
spl_autoload_register(__NAMESPACE__ .'\Foo::test'); // 自 PHP 5.3.0 起
new InexistentClass;
?>
以上例程的输出类似于:
[[Foobar\InexistentClass]]
Fatal error: Class 'Foobar\InexistentClass' not found in ...
参见
- __autoload
spl_autoload_unregister
注销已注册的 __autoload() 函数
说明
bool <span
class="methodname">spl_autoload_unregister ( <span
class="methodparam">mixed
$autoload_function )
从 autoload 自动装载函数队列中移除指定的函数。如果该函数队列处于激活状态,并且在给定函数注销后该队列变为空,则该函数队列将会变为无效。
如果该函数注销后使得自动装载函数队列无效,即使存在有 __autoload 函数它也不会自动激活。
参数
autoload_function
要注销的自动装载函数。
返回值
成功时返回 true, 或者在失败时返回 false。
spl_autoload
__autoload()函数的默认实现
说明
void <span
class="methodname">spl_autoload ( <span
class="methodparam">string
$class_name [, <span
class="type">string $file_extensions ] )
本函数提供了__autoload()的一个默认实现。如果不使用任何参数调用 spl_autoload_register() 函数,则以后在进行 __autoload() 调用时会自动使用此函数。
参数
class_name
file_extensions
在默认情况下,本函数先将类名转换成小写,再在小写的类名后加上 .inc 或
.php 的扩展名作为文件名,然后在所有的包含路径(include
paths)中检查是否存在该文件。
返回值
没有返回值。
spl_classes
返回所有可用的SPL类
说明
array <span class="methodname">spl_classes ( <span class="methodparam">void )
本函数返回当前所有可用的 SPL 类的数组。
参数
此函数没有参数。
返回值
Returns an array containing the currently available SPL classes.
范例
示例 #1 spl_classes example
<?php
print_r(spl_classes());
?>
以上例程的输出类似于:
Array
(
[ArrayObject] => ArrayObject
[ArrayIterator] => ArrayIterator
[CachingIterator] => CachingIterator
[RecursiveCachingIterator] => RecursiveCachingIterator
[DirectoryIterator] => DirectoryIterator
[FilterIterator] => FilterIterator
[LimitIterator] => LimitIterator
[ParentIterator] => ParentIterator
[RecursiveDirectoryIterator] => RecursiveDirectoryIterator
[RecursiveIterator] => RecursiveIterator
[RecursiveIteratorIterator] => RecursiveIteratorIterator
[SeekableIterator] => SeekableIterator
[SimpleXMLIterator] => SimpleXMLIterator
)
spl_object_hash
返回指定对象的hash id
说明
string <span
class="methodname">spl_object_hash ( <span
class="methodparam">object $obj )
本函数为指定对象返回一个唯一标识符。这个标识符可用于作为保存对象或区分不同对象的hash key。
参数
object
Any object.
返回值
字符串,对于每个对象它都是唯一的,并且对同一个对象它总是相同。
范例
示例 #1 A spl_object_hash example
<?php
$id = spl_object_hash($object);
$storage[$id] = $object;
?>
注释
Note:
When an object is destroyed, its hash may be reused for other objects.
spl_object_id
Return the integer object handle for given object
说明
int <span
class="methodname">spl_object_id ( <span
class="methodparam">object $obj )
This function returns a unique identifier for the object. The object id is unique for the lifetime of the object. Once the object is destroyed, its id may be reused for other objects. This behavior is similar to spl_object_hash.
参数
object
Any object.
返回值
An integer identifier that is unique for each currently existing object and is always the same for each object.
范例
示例 #1 A spl_object_id example
<?php
$id = spl_object_id($object);
$storage[$id] = $object;
?>
注释
Note:
When an object is destroyed, its id may be reused for other objects.
目录
- class_implements — 返回指定的类实现的所有接口。
- class_parents — 返回指定类的父类。
- class_uses — Return the traits used by the given class
- iterator_apply — 为迭代器中每个元素调用一个用户自定义函数
- iterator_count — 计算迭代器中元素的个数
- iterator_to_array — 将迭代器中的元素拷贝到数组
- spl_autoload_call — 尝试调用所有已注册的 __autoload() 函数来装载请求类
- spl_autoload_extensions — 注册并返回 spl_autoload 函数使用的默认文件扩展名
- spl_autoload_functions — 返回所有已注册的 __autoload() 函数
- spl_autoload_register — 注册给定的函数作为 __autoload 的实现
- spl_autoload_unregister — 注销已注册的 __autoload() 函数
- spl_autoload — __autoload()函数的默认实现
- spl_classes — 返回所有可用的SPL类
- spl_object_hash — 返回指定对象的hash id
- spl_object_id — Return the integer object handle for given object