📜  PHP |预定义的类和接口

📅  最后修改于: 2022-05-13 01:56:32.167000             🧑  作者: Mango

PHP |预定义的类和接口

有一些类和接口可以是用户定义的,也可以用PHP语言预先定义。

PHP中有 9 个预定义的类和接口:

  • 可遍历
  • 迭代器
  • 迭代器聚合
  • 投掷表
  • 数组访问
  • 可序列化
  • 关闭
  • 发电机

Traversable: Traversable 接口中没有方法,仅作为其他预定义 Traversable 类的基础。通过使用 foreach 构造和该接口,可以很容易地检测到该类是否可遍历。

迭代器:这是一个预定义的接口,用于对象或外部迭代器,它们能够进行内部迭代。该接口扩展了 Traversable 接口。该接口中的五个方法是:

Method nameparametersreturn valuedescription
currentvoidmixedThe present value is returned.
keyvoidscalarThe present value’s key is returned.
nextvoidvoidMoving forward to the next element.
rewindvoidvoid

Set the position of the file pointer to the beginning of the file.

validvoidboolCheck whether an array contains more entries or not.

IteratorAggregate:该接口是 Traversable 接口的扩展,只有一个方法。

method nameparametersreturn typedescription
getIteratorvoidtraversableThis function is used for retrieving an iterator(external).

投掷表

此功能在PHP版本 7 中可用。通过 throw 语句抛出的所有对象都有一个接口,该接口充当称为 Throwtable 的基本接口。异常和错误类使用此接口。

Method namenumber of argumentsname of argumentsreturn typedescription
getMessage0voidstring

Returns the message to be displayed when exception occurs.

getPrevious 0voidthrowtablePrevious exceptions are returned.
getCode 0voidmixedThe code for exception generated.
getFile0voidstringThe file in which exception occurs is returned.
getLine 0voidintThe line number in which exception occurs returned.
getTrace0voidarrayThe stack trace is returned.
getTraceAsString0voidstringThe stack trace is returned in the form of string.
__toString0voidstringThe exception generated is returned in the form of string.

ArrayAccess:此接口允许将对象作为数组进行访问。它有4种方法:

method nameparametersreturnsdescription
offsetExistsoffset value(mixed)boolChecks existence of the offset.
offsetGetoffset value(mixed)mixedThe offset is retrieved through this method.
offsetSetoffset value(mixed), element(mixed)voidA mentioned offset is given some value.
offsetUnsetoffset value(mixed)voidThe value of offset is unset.

序列化:此类允许以自定义方式进行序列化。如果需要对实例进行序列化,则调用方法 serialize()。如果它是在某个方法内的代码中编写的,则不会对程序进行任何更改或副作用。这个接口有两个方法:

method nameparametersreturndescription
serializevoidstringObject is converted into string.
unserializeserialized string (string)voidThe object that was converted into string is converted back to an object.

闭包: PHP中有一些称为匿名函数的函数没有名称。回调参数被赋值为匿名函数。有5种方法:

method nameparametersreturnsdescription
__constructvoidvoidA constructor disallowing instantiation
bind

object (closure)

newthis (object)closure

closureA closure can be duplicated by the class scope and mentioned particular bound object.
bindTonewthis (object)closureclosureA closure can be duplicated by the class scope and new bound object.
 callnewthis (object)closuremixedThe closure is binded and called.
fromCallable Callable (Callable )closureThe callable is converted into closure.

Generator:这个接口是Iterator的扩展,有9个方法:

method nameparametersreturnsdescitption
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 boolVerifies if there was a closing of the iterator.
 __wakeup void void Callback is serialized.