PHP |预定义的类和接口
有一些类和接口可以是用户定义的,也可以用PHP语言预先定义。
PHP中有 9 个预定义的类和接口:
- 可遍历
- 迭代器
- 迭代器聚合
- 投掷表
- 数组访问
- 可序列化
- 关闭
- 发电机
Traversable: Traversable 接口中没有方法,仅作为其他预定义 Traversable 类的基础。通过使用 foreach 构造和该接口,可以很容易地检测到该类是否可遍历。
迭代器:这是一个预定义的接口,用于对象或外部迭代器,它们能够进行内部迭代。该接口扩展了 Traversable 接口。该接口中的五个方法是: Set the position of the file pointer to the beginning of the file.Method name parameters return value description current void mixed The present value is returned. key void scalar The present value’s key is returned. next void void Moving forward to the next element. rewind void void valid void bool Check whether an array contains more entries or not.
IteratorAggregate:该接口是 Traversable 接口的扩展,只有一个方法。method name parameters return type description getIterator void traversable This function is used for retrieving an iterator(external).
投掷表
此功能在PHP版本 7 中可用。通过 throw 语句抛出的所有对象都有一个接口,该接口充当称为 Throwtable 的基本接口。异常和错误类使用此接口。 Returns the message to be displayed when exception occurs.Method name number of arguments name of arguments return type description getMessage 0 void string getPrevious 0 void throwtable Previous exceptions are returned. getCode 0 void mixed The code for exception generated. getFile 0 void string The file in which exception occurs is returned. getLine 0 void int The line number in which exception occurs returned. getTrace 0 void array The stack trace is returned. getTraceAsString 0 void string The stack trace is returned in the form of string. __toString 0 void string The exception generated is returned in the form of string.
ArrayAccess:此接口允许将对象作为数组进行访问。它有4种方法:method name parameters returns description offsetExists offset value(mixed) bool Checks existence of the offset. offsetGet offset value(mixed) mixed The offset is retrieved through this method. offsetSet offset value(mixed), element(mixed) void A mentioned offset is given some value. offsetUnset offset value(mixed) void The value of offset is unset.
可序列化:此类允许以自定义方式进行序列化。如果需要对实例进行序列化,则调用方法 serialize()。如果它是在某个方法内的代码中编写的,则不会对程序进行任何更改或副作用。这个接口有两个方法:method name parameters return description serialize void string Object is converted into string. unserialize serialized string (string) void The object that was converted into string is converted back to an object.
闭包: PHP中有一些称为匿名函数的函数没有名称。回调参数被赋值为匿名函数。有5种方法: object (closure) newthis (object)closuremethod name parameters returns description __construct void void A constructor disallowing instantiation bind closure A closure can be duplicated by the class scope and mentioned particular bound object. bindTo newthis (object)closure closure A closure can be duplicated by the class scope and new bound object. call newthis (object)closure mixed The closure is binded and called. fromCallable Callable (Callable ) closure The callable is converted into closure.
Generator:这个接口是Iterator的扩展,有9个方法:method name parameters returns descitption current void mixed The yielded element can be found. getReturn void mixed The generator’s value is returned by the function. key void mixed The key which was yielded is returned. next void void The generator’s execution is resumed. rewind void void The iterator is rewinded. send value(mixed) mixed A value is sent to the generator. throw exception(throwtable) mixed An exception is thrown into the generator. valid void bool Verifies if there was a closing of the iterator. __wakeup void void Callback is serialized.