3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!defined('PHP_INT_MAX')) { $min = -1; do { $last = $min; } while(0 > $min <<= 1); define('PHP_INT_MIN', $last); define('PHP_INT_MAX', ~$last); } class Object { var $___oid = 0, $___ocmp = 0; function Object() { static $counter = 0; if (PHP_INT_MAX === $this->___oid = ++$counter) { $counter = PHP_INT_MIN; } } function __eq($other) { return $this == $other; } function __neq($other) { return !$this->___eq($other); } function __id(&$other) { if (!isset($other->___ocmp) || $this->___ocmp !== $other->___ocmp) { return false; } $this->___ocmp = $this->___ocmp === 1 ? 2 : 1; return $this->___ocmp === $other->___ocmp; } function __nid(&$other) { return !$this->___id($other); } function __lt($other) { return $this < $other; } function __lteq($other) { return $this <= $other; } function __gt($other) { return !$this->___lteq($other); } function __gteq($other) { return !$this->___lt($other); } function __toString() { return "Object #" . $this->___oid . " of type " . get_class($this); } function __toInt() { return $this->___oid; } function __toFloat() { return (float)$this->___oid; } function __toNumber() { return $this->___oid; } function __toArray() { return get_object_vars($this); } function __toBool() { return true; } function __toScalar() { return $this->___oid; } function &__toClass($class) { if (!is_a($this, $class)) { return null; } return $this; } function compare($op, &$other) { static $fMap = array( '==' => '__eq', '!=' => '__neq', '===' => '__id', '!==' => '__nid', '<' => '__lt', '<=' => '__lteq', '>' => '__gt', '>=' => '__gteq', ); if (!isset($fMap[$op])) { trigger_error("Unknown comparison operator: {$op} called from " . $this->getBacktrace(1, 1), E_USER_ERROR); } return call_user_func(array(&$this, $fMap[$op]), $other); } function getBacktrace($firstLevel = 0, $count = null) { $result = array(); foreach (array_slice(debug_backtrace(), 1 + $firstLevel, $count) as $level) { $func = (!empty($level['class']) ? $level['class'] . $level['type'] : '') . (!empty($level['function']) ? $level['function'] . '()' : ''); if (!$func) { $func = 'UNKNOWN'; } $fileLine = !empty($level['file']) ? "{$level['file']}({$level['line']})" : 'UNKNOWN(-1)'; $result[] = "{$fileLine}: {$func}"; } $result[] = "{main}"; if ($count === 1) { return $result[0]; } else if ($count !== null && count($result) > $count) { return array_slice($result, 0, $count); } return $result; } function &castTo($to = 'string') { $methodName = '__to' . $to; if (!method_exists($this, $methodName)) { if (class_exists($to)) { $methodName = '__toClass'; } else { trigger_error("Unknown cast target: {$to} called from " . $this->getBacktrace(1, 1), E_USER_ERROR); } } return call_user_func(array(&$this, $methodName), $to); } } class A extends Object { } $a = new A; $b = new A; var_dump($a->compare('==', $a), $a->compare('==', $b), $a->compare('===', $a), $a->compare('===', $b));

preferences:
41.44 ms | 402 KiB | 5 Q