3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace SQLQuery; class SQLQuery { private static $methods = array('select', 'update', 'drop', 'delete', 'create', 'alter', 'set'); private static $char_list = array( 'default' => array( 'escape' => '\\', 'quotes' => '"', 'names' => '`', 'name_separator' => '.' ), 'mysqli' => array( 'escape' => '\\', 'quotes' => '"', 'names' => '`', 'name_separator' => '.' ), 'sqlite3' => array( 'escape' => '\\', 'quotes' => '"', 'names' => '`', 'name_separator' => '.' ) ); private $chars = array(); private $type = 'unknown'; private $link = null; function __call($method, $arguments){ if(!in_array($method, self::$methods)) { throw new BadMethodCallException('Method ' . $method . ' does not exist'); } $class = __CLASS__ . ucfirst($method); return new $class($this, $this->chars, $arguments); } function __construct($link = false){ if(is_resource($link)) { $type = get_resource_type($link); if($type !== 'mysql link' || $type !== 'mysql link persistent') { throw new InvalidArgumentException('MySQL resource expected, ' . $type . ' given'); } $this->link = $link; $this->type = 'mysql'; $this->chars = self::$char_list['default']; } else if(is_object($link)) { if(class_exists('PDO') && ($link instanceof \PDO)) { $type = $link->getAttribute(\PDO::ATTR_DRIVER_NAME); } else if(class_exists('mysqli') && ($link instanceof \mysqli)) { $type = 'mysqli'; } else if(class_exists('SQLite3') && ($link instanceof \SQLite3)) { $type = 'sqlite3'; } else { throw new InvalidArgumentException('Unsupported connection type ' . get_class($link)); } $this->link = $link; $this->type = $type; $this->chars = self::$char_list[$type]; } else throw new InvalidArgumentException('Resource or Object excepted, ' . gettype($link) . ' given'); } } if(!@class_alias('SQLQuery\\SQLQuery', '\\SQLQuery')) { // very ugly hack eval('class SQLQuery extends \SQLQuery\SQLQuery{};'); } final class SQLQuerySelect { function __construct(SQLQuery $sql, array $chars, $arguments){ var_dump($chars, $arguments); } } // --------------------------------- $x = new \SQLQuery(new \SQLite3()); $x->select('a');

preferences:
51.67 ms | 402 KiB | 5 Q