找到 34423 篇文章,关于编程

PHP WeakReference 类

Malhar Lathkar
更新于 2020年9月21日 12:06:26

368 次浏览

简介使用弱引用,可以保留对对象的引用,而不会阻止对象的销毁。可以使用弱引用实现类似缓存的结构。弱引用类似于普通引用,只是它不会阻止垃圾回收器回收对象。如果找不到对该对象的强引用,它将立即从内存中删除。这样就可以在没有内存问题的情况下实现缓存的大部分好处。PHP 7.4 中引入了 WeakReference 类。在此版本之前,同样的效果是通过…… 阅读更多

PHP Traversable 接口

Malhar Lathkar
更新于 2020年9月21日 12:04:55

364 次浏览

简介Traversable 是一个抽象接口,因此任何类都不能直接实现它。通常,扩展 Traversable 的 Iterator 或 IteratorAggregate 接口用于检查实现类是否可以使用 PHP 中的 foreach 结构进行遍历。某些实现此接口的内置类可以在 foreach 中使用,并且不需要实现迭代器接口。由于 Traversable 是一个抽象接口,因此它没有任何方法。语法Traversable {    // }

PHP Throwable 接口

Malhar Lathkar
更新于 2020年9月21日 12:02:37

433 次浏览

简介在 PHP 7 中,Throwable 接口充当任何可以作为 throw 语句参数的对象的基础,包括 Error 和 Exception。Error 和 Exception 类(预定义和用户定义的错误和异常类分别派生自它们)都实现了 Throwable 接口。Throwable 接口中定义了以下抽象方法 −语法Throwable {    /* 方法 */    abstract public getMessage ( void ) : string    abstract public getCode ( void ) : int    abstract public getFile ( void ) : string    abstract public getLine ( void ) : int    abstract public getTrace ( void ... 阅读更多

PHP Serializable 接口

Malhar Lathkar
更新于 2020年9月21日 11:51:42

531 次浏览

简介Serializable 接口存在于 PHP 库中,用于构建提供自定义序列化的类。PHP 的 serialize() 函数能够将大多数值序列化为可存储的表示形式。但是,用户定义类的对象无法序列化。此接口使其成为可能。语法Serializable {    /* 方法 */    abstract public serialize ( void ) : string    abstract public unserialize ( string $serialized ) : void }方法Serializable::serialize — 对象的字符串表示Serializable::unserialize — 从序列化的字符串表示形式构造对象内置的 serialze() 函数生成值的可存储表示形式serialize ( mixed $value ) : stringunserialize() ... 阅读更多

PHP IteratorAggregate 接口

Malhar Lathkar
更新于 2020年9月21日 11:49:43

400 次浏览

简介IteratorAggregate 接口扩展了抽象 Traversable 接口。类实现它以创建外部迭代器。此接口引入了一个名为 getIterator 的抽象方法。语法IteratorAggregate extends Traversable {    /* 方法 */    abstract public getIterator ( void ) : Traversable }方法IteratorAggregate::getIterator — 获取外部迭代器此函数没有参数,并返回实现 Iterator 或 Traversable 的对象的实例。IteratorAggregate 示例在下面的 PHP 脚本中,实现 IteratorAggregate 接口的类包含一个数组作为属性getIterator() 方法从此数组中返回 ArrayIterator 对象。我们可以使用 foreach 循环遍历数组。示例 实时演示输出数组属性的遍历显示以下结果0=>10 1=>20 2=>30 3=>40

PHP Iterable 接口

Malhar Lathkar
更新于 2020年9月21日 11:48:02

792 次浏览

简介Iterator 接口扩展了抽象 Traversable 接口。PHP 为许多例行功能提供了许多内置迭代器(称为 SPL 迭代器)。例如 ArrayIterator、DirectoryIterator 等。实现 Iterator 接口的用户类应实现其中定义的抽象方法。语法Iterator extends Traversable {    /* 方法 */    abstract public current ( void ) : mixed    abstract public key ( void ) : scalar    abstract public next ( void ) : void    abstract public rewind ( void ) : void    abstract public valid ( void ) : bool }方法Iterator::current — 返回当前元素Iterator::key — 返回... 阅读更多

PHP Generator 类

Malhar Lathkar
更新于 2020年9月21日 11:45:54

649 次浏览

简介使用 foreach 等循环结构遍历大型数据集需要大量的内存和相当大的处理时间。使用生成器,可以迭代一组数据而无需这些开销。生成器函数类似于普通函数。但是,函数中没有 return 语句,生成器使用 yield 关键字重复执行,以便它提供要迭代的值。yield 关键字是生成器机制的核心。尽管它的使用看起来类似于 return,但它不会停止函数的执行。它提供下一个迭代值并暂停... 阅读更多

PHP Closure 类

Malhar Lathkar
更新于 2020年9月21日 11:43:49

1K+ 次浏览

简介匿名函数(也称为 lambda)返回 Closure 类的对象。此类有一些附加方法,可以进一步控制匿名函数。语法Closure {    /* 方法 */    private __construct ( void )    public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure    public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure    public call ( object $newthis [, mixed $... ] ) : mixed    public static fromCallable ( callable $callable ) : Closure }方法private Closure::__construct ( void ) — 此方法存在... 阅读更多

PHP ArrayAcccess 接口

Malhar Lathkar
更新于 2020年9月21日 11:42:01

162 次浏览

简介在 PHP 中,ArrayAccess 接口用于开发一个类,该类提供对作为数组的属性之一的类似数组的访问。可以在对象创建期间不公开此类数组属性的情况下对其进行操作。ArrayAccess 接口定义了以下抽象方法语法ArrayAccess {    /* 方法 */    abstract public offsetExists ( mixed $offset ) : bool    abstract public offsetGet ( mixed $offset ) : mixed    abstract public offsetSet ( mixed $offset , mixed $value ) : void    abstract public offsetUnset ( mixed $offset ) : void }方法ArrayAccess::offsetExists − 是否存在偏移量ArrayAccess::offsetGet − 要检索的偏移量ArrayAccess::offsetSet − 分配... 阅读更多

PHP ArgumentCountError

Malhar Lathkar
更新于 2020年9月21日 11:37:53

449 次浏览

简介当传递给用户定义函数或方法的参数少于其定义中的参数时,PHP 解析器会抛出 ArgumentCountError。ArgumentCountError 类继承自 TypeError 类ArgumentCountError 示例在以下示例中,定义了用户定义函数 add() 以接收两个参数。但是,如果在调用时提供的参数少于所需数量,则将抛出 ArgumentCountError,可以使用 catch 块处理它。示例 实时演示输出这将产生以下结果:函数 add() 的参数太少,在 C:\xampp\php\test.php 的第 6 行传递了 1 个参数,而预期为 2 个。在以下示例中,myclass 中的 setdata() 方法定义为具有两个形式参数。... 阅读更多

广告
© . All rights reserved.