3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Query { protected $table = ''; protected $querystring = ''; public function __construct($pdo = '') { $this->connection = $pdo; return $this; } public function query($query) { $this->querystring = $query; return $this->execute(); } public function execute() { $this->querystring = str_replace([':fields', ':where', ':orderby', ':limit'], ['*', '', '', ''], $this->querystring); return $this->connection->query($this->querystring); } public function safeVar($var) { if (is_int($var)) { return (int) $var; } elseif (is_string($var)) { return '"'.(string) $var.'"'; } elseif (is_array($var)) { return '"'.implode(', ', $var).'"'; } return $var; } public function where(...$rules) { if (!strpos($this->querystring, ':where')) { return false; } $where = ''; foreach ($rules as $rule) { if (!isset($rule['field']) || !isset($rule['way']) || !isset($rule['against'])) { if (!isset($rule[0]) || !isset($rule[1]) || !isset($rule[2])) { return false; } else { $where .= " AND `{$rule[0]}` {$rule[1]} {$rule[2]}"; } } else { $where .= " AND `{$rule['field']}` {$rule['way']} ".safeVar($rule['against']).""; } } $this->querystring = str_replace(':where', ' WHERE '.trim($where, ' AND '), $this->querystring); return $this; } public function orderby($field, $way = 'DESC') { if (!strpos($this->querystring, ':orderby')) { return false; } $way = strtoupper((string) $way); if (!in_array($way, ['DESC', 'ASC'])) { $way = 'DESC'; } $this->querystring = str_replace(':orderby', "ORDERBY `{$field}` {$way}", $this->querystring); return $this; } public function limit($from, $to) { if (!strpos($this->querystring, ':limit')) { return false; } $from = (int) $from; $to = (int) $to; $this->querystring = str_replace(':limit', "LIMIT {$from}, {$to}", $this->querystring); return $this; } public function __toString() { return $this->querystring; } } class Select extends Query { public function __construct(\PDO $pdo, $table) { parent::__construct($pdo); $this->table = $table; $this->querystring = "SELECT :fields FROM {$table}:where:orderby:limit;"; } public function get(...$fields = ['*']) { if (!strpos($this->querystring, ':fields')) { return false; } $this->querystring = str_replace(':fields', implode(', ', $fields), $this->querystring); return $this; } public function first() { if (!strpos($this->querystring, ':limit')) { return false; } $this->querystring = str_replace(':limit', 'LIMIT 0, 1', $this->querystring); return $this; } } class Update extends Query { public function __construct(\PDO $pdo, $table) { parent::__construct($pdo); $this->table = $table; $this->querystring = 'UPDATE :table SET :values:where:orderby:limit;'; } } class Delete extends Query { public function __construct(\PDO $pdo, $table) { parent::__construct($pdo); $this->table = $table; $this->querystring = 'DELETE FROM :table:where:orderby:limit;'; } } class Insert extends Query { public function __construct(\PDO $pdo, $table) { parent::__construct($pdo); $this->table = $table; $this->querystring = 'INSERT INTO :table:fields VALUES :values;'; } } $select = new Select('', 'test'); $update = new Update('', 'test'); $delete = new Delete('', 'test'); $insert = new Insert('', 'test');
Output for 5.4.0 - 5.4.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/krhVS on line 38
Process exited with code 255.
Output for 5.2.14 - 5.2.17, 5.3.3 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/krhVS on line 22
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.13, 5.3.0 - 5.3.2
Strict Standards: Redefining already defined constructor for class Query in /in/krhVS on line 14 Parse error: syntax error, unexpected '[', expecting ')' in /in/krhVS on line 22
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Strict Standards: Redefining already defined constructor for class Query in /in/krhVS on line 14 Parse error: parse error, unexpected '[', expecting ')' in /in/krhVS on line 22
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/krhVS 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_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/krhVS 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/krhVS on line 5
Process exited with code 255.

preferences:
218.17 ms | 1395 KiB | 125 Q