3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * AbstractError.php * * @package WordPress\Error */ namespace WordPress\Error; use InvalidArgumentException; use RuntimeException; /** * WordPress Errors extend {@link RuntimeException} with relevant data. * * Extend this class with domain-specific codes and messages and call this * constructor to include the instance in the list of recorded errors. * * Use {@link is_wp_error()} to check if this class is returned. Many core * WordPress functions pass this class in the event of an error and if not * handled properly will result in code errors. * * @package WordPress\Error * @since 2.1.0 */ abstract class AbstractError extends RuntimeException { /** * Internal map of code messages * * @var array code (int) => message (string) */ protected static $codeMessages = []; /** * Internal list of AbstractError instances * * @var self[] */ private static $instances = []; /** * Optional error data * * @var mixed */ private $data; /** * Initialize the error * * @param int $code an error code * @param mixed $data optional error data * @param self $previous optional previous error * * @throws InvalidArgumentException when the code is not recognized */ public function __construct($code, $data = null, self $previous = null) { if (!isset(static::$codeMessages[$code])) { throw new InvalidArgumentException("Invalid error code [$code]"); } parent::__construct(static::$codeMessages[$code], $code, $previous); $this->data = $data; self::$instances[] = $this; } /** * Remove all instances having the specified code * * @param int $code an error code to remove */ final public static function remove($code) { foreach (self::$instances as $index => $error) { if ($code === $error->getCode()) { unset(self::$instances[$index]); } } } /** * Retrieve all instances having the specified code * * @param int $code an error code * * @yield self each error instance with matching code */ final public static function get($code) { foreach (self::$instances as $error) { if ($code === $error->getCode()) { yield $error; } } } /** * Retrieve all error instances * * @yield self each error instance */ final public static function getAll() { foreach (self::$instances as $error) { yield $error; } } /** * Retrieve the error data * * @return mixed error data, if available */ public function getData() { return $this->data; } /** * Replace the error data * * @param mixed $data optional error data * * @return self an instance with the new data */ public function setData($data = null) { return new static($this->getCode(), $data, $this); } /** * Triggers a PHP error * * @param int $type optional PHP error type */ public function trigger($type = E_USER_NOTICE) { trigger_error((string) $this, (int) $type); } /** * Converts the object to a string * * @return string a formatted error string */ public function __toString() { return "Error " . $this->getCode() . ": " . $this->getMessage(); } } class E extends AbstractError { protected static $codeMessages = [ 5001 => 'foo', 6001 => 'bar' ]; public static function foo($data = null) { return new static(5001, $data); } public static function bar($data = null) { return new static(6001, $data); } } $e = E::foo(); $s = $e->setData('snowflake'); foreach (AbstractError::getAll() as $error) { echo $error->getMessage() . PHP_EOL; } var_dump($e, $s);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
foo foo object(WordPress\Error\E)#1 (8) { ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(162) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(171) ["function"]=> string(3) "foo" ["class"]=> string(17) "WordPress\Error\E" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["data":"WordPress\Error\AbstractError":private]=> NULL } object(WordPress\Error\E)#2 (8) { ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(129) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(172) ["function"]=> string(7) "setData" ["class"]=> string(29) "WordPress\Error\AbstractError" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(9) "snowflake" } } } ["previous":"Exception":private]=> object(WordPress\Error\E)#1 (8) { ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(162) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(171) ["function"]=> string(3) "foo" ["class"]=> string(17) "WordPress\Error\E" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["data":"WordPress\Error\AbstractError":private]=> NULL } ["data":"WordPress\Error\AbstractError":private]=> string(9) "snowflake" }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 foo foo object(WordPress\Error\E)#1 (8) { ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(162) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(171) ["function"]=> string(3) "foo" ["class"]=> string(17) "WordPress\Error\E" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["data":"WordPress\Error\AbstractError":private]=> NULL } object(WordPress\Error\E)#2 (8) { ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(129) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(172) ["function"]=> string(7) "setData" ["class"]=> string(29) "WordPress\Error\AbstractError" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(9) "snowflake" } } } ["previous":"Exception":private]=> object(WordPress\Error\E)#1 (8) { ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(162) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(171) ["function"]=> string(3) "foo" ["class"]=> string(17) "WordPress\Error\E" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL ["data":"WordPress\Error\AbstractError":private]=> NULL } ["data":"WordPress\Error\AbstractError":private]=> string(9) "snowflake" }
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
foo foo object(WordPress\Error\E)#1 (8) { ["data":"WordPress\Error\AbstractError":private]=> NULL ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(162) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(171) ["function"]=> string(3) "foo" ["class"]=> string(17) "WordPress\Error\E" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } object(WordPress\Error\E)#2 (8) { ["data":"WordPress\Error\AbstractError":private]=> string(9) "snowflake" ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(129) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(172) ["function"]=> string(7) "setData" ["class"]=> string(29) "WordPress\Error\AbstractError" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(9) "snowflake" } } } ["previous":"Exception":private]=> object(WordPress\Error\E)#1 (8) { ["data":"WordPress\Error\AbstractError":private]=> NULL ["message":protected]=> string(3) "foo" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(5001) ["file":protected]=> string(9) "/in/8hVFP" ["line":protected]=> int(162) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/8hVFP" ["line"]=> int(171) ["function"]=> string(3) "foo" ["class"]=> string(17) "WordPress\Error\E" ["type"]=> string(2) "::" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } }
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$error' (T_VARIABLE) in /in/8hVFP on line 93
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/8hVFP on line 33
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/8hVFP on line 8
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/8hVFP on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/8hVFP on line 8
Process exited with code 255.

preferences:
280.26 ms | 401 KiB | 342 Q