PHP 7.2のWarn when counting non-countable typesを試した
PHP 7.2でcount関数にCountableタイプ以外を指定するとWarningが出るようになったということで試してみた。
<?php echo count([1]); // 1 echo count([1, 2, 3]); // 3 echo count([1, null]); // 2 echo count(["one" => 1, "two" => 2]); // 2 echo count(null); // Warning echo count(new StdClass); // Warning echo count(1); // Warning echo count("1"); // Warning class FullName implements Countable { private $firstName; private $middleName; private $lastName; public function __construct(?string $firstName, ?string $middleName, ?string $lastName) { $this->firstName = $firstName; $this->middleName = $middleName; $this->lastName = $lastName; } public function count() : int { $count = 0; foreach (get_object_vars($this) as $val) { if ($val) { $count++; } } return $count; } } // 3 echo count(new FullName("hogeta", "hogehoge", "hogeo"));
Countableのinterfaceを実装し、count関数にインスタンスを指定するとそのクラスのcountメソッドがコールされる仕組み。
Countable自体使うのはお初だったのでその動きを知れたのはよかった。