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));
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Cannot use 'Object' as class name as it is reserved in /in/vYJoh on line 13
Process exited with code 255.
Output for 7.1.0 - 7.1.25
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Object has a deprecated constructor in /in/vYJoh on line 13 Warning: Parameter 1 to Object::__id() expected to be a reference, value given in /in/vYJoh on line 131 Warning: Parameter 1 to Object::__id() expected to be a reference, value given in /in/vYJoh on line 131 bool(true) bool(false) bool(true) bool(false)
Output for 7.0.0 - 7.0.8, 7.0.10 - 7.0.33
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Object has a deprecated constructor in /in/vYJoh on line 13 bool(true) bool(false) bool(true) bool(false)
Output for 7.0.9
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Object has a deprecated constructor in /in/vYJoh on line 13 Warning: Parameter 1 to Object::__id() expected to be a reference, value given in /in/vYJoh on line 131 Warning: Parameter 1 to Object::__id() expected to be a reference, value given in /in/vYJoh on line 131 bool(true) bool(false) NULL NULL
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
bool(true) bool(false) bool(true) bool(false)

preferences:
205.52 ms | 402 KiB | 340 Q