3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Maniple_Model { const CAMELIZE = 0; const UNDERSCORE = 1; /** * @param array $data OPTIONAL * @return void */ public function __construct(array $data = null) // {{{ { if ($data) { $this->setFromArray($data); } } // }}} /** * @param array $data * @return Maniple_Model */ public function setFromArray(array $data) // {{{ { foreach ($data as $key => $value) { $this->__set($key, $value, false); } return $this; } // }}} /** * @return array */ public function toArray($keyTransform = self::CAMELIZE) { $array = array(); foreach (get_object_vars($this) as $property) { // include only properties starting with an underscore, // remove the underscore before retrieving property value if ('_' !== $property[0]) { continue; } $property = substr($property, 1); } return $array; } /** * @param string $key * @param mixed $value * @param bool $throw OPTIONAL * @return Maniple_Model * @throws InvalidArgumentException */ protected function _setProperty($key, $value, $throw = true) // {{{ { $key = self::camelize($key); $setter = 'set' . $key; if (method_exists($this, $setter)) { $this->{$setter}($value); return $this; } $property = '_' . $key; if (property_exists($this, $property)) { $this->{$property} = $value; return $this; } throw new InvalidArgumentException(sprintf('Invalid property: %s', $key)); return false; } // }}} /** * @param string $key * @param bool $throw OPTIONAL * @return mixed * @throws InvalidArgumentException */ protected function _getProperty($key, $throw = true) // {{{ { $key = self::camelize($key); $getter = 'get' . $key; if (method_exists($this, $getter)) { return $this->{$getter}(); } $property = '_' . $key; if (property_exists($this, $property)) { return $this->{$property}; } if ($throw) { throw new InvalidArgumentException(sprintf('Invalid property: %s', $key)); } } // }}} /** * @param string $key * @return bool */ public function has($key) // {{{ { return property_exists($this, '_' . self::camelize($key)); } // }}} /** * @param string $key * @return bool */ public function __isset($key) // {{{ { return isset($this->{'_' . self::camelize($key)}); } // }}} /** * @param string $key * @return mixed * @throws InvalidArgumentException */ public function __get($key) // {{{ { return $this->_getProperty($key, true); } // }}} /** * @param string $key * @param mixed $value * @return void * @throws InvalidArgumentException */ public function __set($key, $value) // {{{ { return $this->_setProperty($key, $value, true); } // }}} /** * Transform given string to camel-case. * * @param string $str * @return string */ public static function camelize($str) // {{{ { if (is_array($str)) { return strtoupper($str[1]); } return preg_replace_callback( '/_(\w)/', array(__CLASS__, __FUNCTION__), (string) $str ); } // }}} /** * Transform given string to underscore separated notation. * * @param string $str * @return string */ public static function underscore($str) { if (is_array($str)) { return $str[1][0] . '_' . strtolower($str[1][1]); } return preg_replace_callback( '/([a-z][A-Z])/', array(__CLASS__, __FUNCTION__), (string) $str ); } /** * @param string $string * @param int $transform * @return string */ public static function transform($string, $transform) // {{{ { switch ($transform) { case self::CAMELIZE: $string = self::camelize($string); break; case self::UNDERSCORE: $string = self::underscore($string); break; } return (string) $string; } // }}} } echo Maniple_Model::camelize('active_record'), "\n"; echo Maniple_Model::camelize('ActiveRecord'), "\n"; echo Maniple_Model::underscore('active_record'), "\n"; echo Maniple_Model::underscore('ActiveRecord'), "\n";
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 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
activeRecord ActiveRecord active_record Active_record
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/LYU7p on line 12
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/LYU7p on line 5
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
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/LYU7p on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/LYU7p on line 5
Process exited with code 255.

preferences:
213.23 ms | 401 KiB | 330 Q