21. さまざまな問題 [3/3]
演算子に対応するバイトコード命令があるとは限らない
たとえば $a > $b
2018-06-15 PHPカンファレンス福岡 非公式前夜祭 21
__is_greater() and __is_greater_or_equal() require a rebuild of the main PHP runtime
using the included patch. Without this patch, $a > $b is automatically remapped to $b < $a
by the engine.
https://github.com/php/pecl-php-operator
◦ PHP ではコンパイラが $a > $b を $b < $a に変換する
◦ PECL operator では元々どちらだったかを記録することで対処
26. Comparable interface for PHP
https://github.com/nikic/comparable
このアプローチで Comparable インタフェースを定義
2018-06-15 PHPカンファレンス福岡 非公式前夜祭 26
<?php
class MyClass implements Comparable
{
public static function compare($a, $b) {
...
}
}
◦ Comparable インタフェースを実装するクラスを定義する
◦ そのクラスのオブジェクトの比較では compare メソッドが使われる
27. これは実用になる?
そういう感じではない
2018-06-15 PHPカンファレンス福岡 非公式前夜祭 27
Note: This is to the most part just code demonstrating the implementation of a magic interface
for a tutorial. I do not currently plan on proposing including such an interface for PHP itself.
https://github.com/nikic/comparable
いくつかの問題
◦ そもそも上記のとおり実用を意図していない
◦ PHP 7 に対応していない
◦ 比較演算しか変えられない
◦ この拡張を真似して実装しても任意の演算子の処理を定義できるようにはならない
◦ zend_object_handlers で管理されている操作だけ