3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php const VALUE_FORMAT_RAW = "raw"; const VALUE_FORMAT_TAG_NAME_VALUE_ASSOC = "tagNameValueAssoc"; const VALUE_FORMAT_ARRAY = "array"; function formatArgumentValue(SimpleXMLElement $node, $options) { switch ($options["format"]) { // Associative array from each children of the target node that have the tag name as key and // the tag value as value case VALUE_FORMAT_TAG_NAME_VALUE_ASSOC: $assoc = []; // Recursively get the associative array for the node foreach ($node->children() as $nodePart) { if($nodePart->count() > 0) { $format = VALUE_FORMAT_TAG_NAME_VALUE_ASSOC; if (!empty($nodePart->attributes()->type) && $nodePart->attributes()->type == "array") { $format = VALUE_FORMAT_ARRAY; } $assoc[$nodePart->getName()] = $this->formatArgumentValue($nodePart, ["format" => $format]); } else { // If the value is already defined, we say that the value is an array and add the new value to it if(!empty($assoc[$nodePart->getName()])) { if(!is_array($assoc[$nodePart->getName()])) { $assoc[$nodePart->getName()] = [$assoc[$nodePart->getName()]]; } $assoc[$nodePart->getName()][] = $this->resolveValueString((string) $nodePart); } else { $assoc[$nodePart->getName()] = $this->resolveValueString((string) $nodePart); } } } $argumentValue = $assoc; break; case VALUE_FORMAT_ARRAY: $children = []; $i = 0; foreach($node->children() as $child) { // Get the path for the key $key = $i++; // Set the default key as a regular numbered autoincremented index // NOTE: Here a post-increment is done, the value copied to $key is $i BEFORE // it being incremented. The array will start at 0, even if the increment is done at // the beginning of the string. if(!empty($options['childrenKey'])) { $key = (string) $this->walkNodePath($options['childrenKey'], $child); } $children[$key] = $this->formatArgumentValue($child, ["format" => $options["childrenFormat"]]); } $argumentValue = $children; break; // Raw string value of the tag as value. Default. case VALUE_FORMAT_RAW: default: $argumentValue = $this->resolveValueString((string) $node); break; } return $argumentValue; } $xml = <<<EOL <config> <formatters> <fileformatter> <class>Monolog\Formatter\LineFormatter</class> <format>[%datetime%] %level_name%: %message%&#xA;</format> </fileformatter> <mongoformatter> <class>Bettr\Service\Log\Formatter\MongoDBFormatter</class> </mongoformatter> </formatters> <handlers> <filelog> <class>Monolog\Handler\StreamHandler</class> <level>DEBUG</level> <formatter>fileformatter</formatter> <processors>psr_processor</processors> <stream>../system/var/logs/debug.log</stream> </filelog> <mongolog> <class>Bettr\Service\Log\Handler\MongoServiceHandler</class> <level>ERROR</level> <formatter>mongoformatter</formatter> <mongoManager>{service:mongo}</mongoManager> <processors type="array">psr_processor</processors> <collection>logger</collection> </mongolog> </handlers> <processors> <psr_processor> <class>Monolog\Processor\PsrLogMessageProcessor</class> </psr_processor> </processors> <loggers> <bettr> <handlers type="array"> <handler>filelog</handler> <handler>mongolog</handler> </handlers> </bettr> </loggers> </config> EOL; $simpleXMLElement = new SimpleXMLElement($xml); var_dump(formatArgumentValue($simpleXMLElement, ["format" => VALUE_FORMAT_TAG_NAME_VALUE_ASSOC]));
Output for 7.0.30 - 7.3.0beta1
Fatal error: Uncaught Error: Using $this when not in object context in /in/ERLft:24 Stack trace: #0 /in/ERLft(124): formatArgumentValue(Object(SimpleXMLElement), Array) #1 {main} thrown in /in/ERLft on line 24
Process exited with code 255.
Output for hhvm-3.22.0
Notice: Undefined variable: this in /in/ERLft on line -1 Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function formatArgumentValue() on a non-object (null)' in /in/ERLft:-1 Stack trace: #0 /in/ERLft(-1): formatArgumentValue() #1 {main}
Process exited with code 255.
Output for hhvm-3.18.5 - 3.21.3
Notice: Undefined variable: this in /in/ERLft on line 24 Fatal error: Uncaught exception 'BadMethodCallException' with message 'Call to a member function formatArgumentValue() on a non-object (null)' in /in/ERLft:24 Stack trace: #0 /in/ERLft(124): formatArgumentValue() #1 {main}
Process exited with code 255.
Output for 5.6.30
Fatal error: Using $this when not in object context in /in/ERLft on line 24
Process exited with code 255.