实现匿名类

PHP 7引入了一个新功能,匿名类。 与匿名函数非常相似,可以将匿名类定义为表达式的一部分,从而创建一个没有名称的类。 匿名类用于需要动态创建对象的情况,使用该对象然后将其丢弃。

如何做…

1.stdClass的替代方法是定义一个匿名类。

在定义中,您可以定义任何属性和方法(包括魔术方法)。 在此示例中,我们定义了一个具有两个属性和一个魔术方法 __construct() 的匿名类:

  1. $a = new class (123.45, 'TEST') {
  2. public $total = 0;
  3. public $test = '';
  4. public function __construct($total, $test)
  5. {
  6. $this->total = $total;
  7. $this->test = $test;
  8. }
  9. };

2.匿名类可以扩展任何类。

在此示例中,一个匿名类扩展了 FilterIterator,并覆盖了 __construct()accept() 方法。 作为参数,它接受 ArrayIterator $b,它表示 10 到 100 的数组,以 10 为增量。第二个参数用作输出的限制:

  1. $b = new ArrayIterator(range(10,100,10));
  2. $f = new class ($b, 50) extends FilterIterator {
  3. public $limit = 0;
  4. public function __construct($iterator, $limit)
  5. {
  6. $this->limit = $limit;
  7. parent::__construct($iterator);
  8. }
  9. public function accept()
  10. {
  11. return ($this->current() <= $this->limit);
  12. }
  13. };

3.匿名类可以实现接口。

在此示例中,匿名类用于生成 HTML 颜色代码图表。 该类实现内置的PHP Countable 接口。 定义了 count() 方法,当此类与需要 Countable 的方法或函数一起使用时,将调用此方法:

  1. define('MAX_COLORS', 256 ** 3);
  2. $d = new class () implements Countable {
  3. public $current = 0;
  4. public $maxRows = 16;
  5. public $maxCols = 64;
  6. public function cycle()
  7. {
  8. $row = '';
  9. $max = $this->maxRows * $this->maxCols;
  10. for ($x = 0; $x < $this->maxRows; $x++) {
  11. $row .= '<tr>';
  12. for ($y = 0; $y < $this->maxCols; $y++) {
  13. $row .= sprintf(
  14. '<td style="background-color: #%06X;"',
  15. $this->current);
  16. $row .= sprintf(
  17. 'title="#%06X">&nbsp;</td>',
  18. $this->current);
  19. $this->current++;
  20. $this->current = ($this->current >MAX_COLORS) ? 0
  21. : $this->current;
  22. }
  23. $row .= '</tr>';
  24. }
  25. return $row;
  26. }
  27. public function count()
  28. {
  29. return MAX_COLORS;
  30. }
  31. };
  1. 匿名类可以使用特性。

5.最后这个例子是对前面定义的例子的修改。我们没有定义 Test 类,而是定义了匿名类:

  1. $a = new class() {
  2. use IdTrait, NameTrait {
  3. NameTrait::setKeyinsteadofIdTrait;
  4. IdTrait::setKey as setKeyDate;
  5. }
  6. };

如何运行…

在匿名类中,你可以定义任何属性或方法。使用前面的例子,你可以定义一个匿名类,它可以接受构造函数的参数,并且可以访问属性。将步骤2中描述的代码放入测试脚本 chap_04_oop_anonymous_class.php 中。添加以下这些 echo 语句:

  1. echo "\nAnonymous Class\n";
  2. echo $a->total .PHP_EOL;
  3. echo $a->test . PHP_EOL;

下面是匿名类的输出。

实现匿名类 - 图1

为了使用 FilterIterator,必须重写 accept() 方法。 在此方法中,您定义了将迭代元素包含为输出的条件。 现在继续,并将步骤4中的代码添加到测试脚本中。 然后,您可以添加以下 echo 语句以测试匿名类:

  1. echo "\nAnonymous Class Extends FilterIterator\n";
  2. foreach ($f as $item) echo $item . '';
  3. echo PHP_EOL;

在此示例中,限制为 50。 原始 ArrayIterator 包含一个 10 到 100 的值数组,以 10 为增量,如以下输出所示:

实现匿名类 - 图2

要了解一个实现了接口的匿名类,请看步骤5和6所示的例子。将这段代码放在一个文件中,chap_04_oop_anonymous_class_interfaces.php

接下来,添加使您可以在HTML颜色图表中进行分页的代码:

  1. $d->current = $_GET['current'] ?? 0;
  2. $d->current = hexdec($d->current);
  3. $factor = ($d->maxRows * $d->maxCols);
  4. $next = $d->current + $factor;
  5. $prev = $d->current - $factor;
  6. $next = ($next <MAX_COLORS) ? $next : MAX_COLORS - $factor;
  7. $prev = ($prev>= 0) ? $prev : 0;
  8. $next = sprintf('%06X', $next);
  9. $prev = sprintf('%06X', $prev);
  10. ?>

最后,继续把HTML彩图以网页的形式呈现出来:

  1. <h1>Total Possible Color Combinations: <?= count($d); ?></h1>
  2. <hr>
  3. <table>
  4. <?= $d->cycle(); ?>
  5. </table>
  6. <a href="?current=<?= $prev ?>"><<PREV</a>
  7. <a href="?current=<?= $next ?>">NEXT >></a>

请注意,您可以通过将匿名类的实例传递到 count() 函数中来利用 Countable 接口(如标签之间所示)。下面是浏览器窗口中显示的输出:

实现匿名类 - 图3

最后,为了说明特性在匿名类中的使用,请将上一节中提到的chap_04_oop_trait_multiple.php 文件复制到新文件 chap_04_oop_trait_anonymous_class.php 中。 删除 Test 类的定义,并将其替换为匿名类:

  1. $a = new class() {
  2. use IdTrait, NameTrait {
  3. NameTrait::setKeyinsteadofIdTrait;
  4. IdTrait::setKey as setKeyDate;
  5. }
  6. };

删除此行:

  1. $a = new Test();

运行代码时,您将看到与前面的屏幕快照完全相同的输出,但类引用将是匿名的:

实现匿名类 - 图4