General Discussion > Web Programming and Design

Your favorite web design tools/tips/tricks/tutorials/sites

<< < (9/10) > >>

Eeems:
Using ReflectionClass can be fun :)
I use it for argument pass-off in my sql class.

--- Code: ---<?php
/**
* SQL class. Used for handling SQL connections
*
* @module sql
* @class SQL
* @constructor
*/
class SQL {
/**
* This is the mysqli connection beneath everything

* @property sql
* @type {mysqli}
* @private
* @required
*/
private $sql;
public function __construct($server,$user,$pass,$db){
$this->sql = new mysqli($server,$user,$pass,$db) or die('Unable to connect to mysql');
}
public function __invoke(){
return $this->sql;
}
public function __get($name){
switch($name){
case 'error':
return $this->sql->error;
break;
}
}
/**
* Returns a Query object based on inputs
*
* @method query
* @param {String} sql The sql expression to run
* @param {String=null} [types] A string containing all the types of arguments being passed
* @param {Mixed} [bindings]* The bindings to use in the sql statement
* @return {Query} Returns the query object
*/
public function query(){
$reflect = new ReflectionClass('Query');
$args = array_merge(array($this),func_get_args());
return $reflect->newInstanceArgs($args);
}
public function escape($s){
return $this->sql->escape_string($s);
}
public function charset($charset){
return $this->sql->set_charset($charset);
}
}
/**
* Query class. Returned by SQL::query()
*
* @class Query
* @constructor
*/
class Query {
private $query;
private $sql;
public function __construct($sql,$source,$types=null){
$args = func_get_args();
$args = array_splice($args,2);
$this->sql = $sql();
$this->query = $sql()->prepare($source);
if(!is_null($types)){
call_user_func_array(array($this->query, 'bind_param'),make_referenced($args)) or die($sql()->error);
}
}
public function __invoke(){
return $this->query;
}
public function execute(){
if($this->query){
$r = $this->query->execute();
$this->sql->commit();
return $r;
}else{
return false;
}
}
public function __get($name){
switch($name){
/**
* Returns the mysqli::results object for the
* query

* @property results
* @type {mysqli::results}
* @public
*/
case 'results':
if($this->query){
$this->execute();
$result = $this->query->get_result();
$this->query->close();
return $result;
}else{
return false;
}
break;
/**
* Returns an associative array of the query resulsts

* @property assoc_results
* @type {Array}
* @public
*/
/**
* Returns an associative array of the query resulsts

* @property resulsts_assoc
* @type {Array}
* @public
*/
case 'assoc_results':case 'results_assoc':
if($this->query){
$a = array();
$r = $this->results;
while($row = $r->fetch_assoc()){
array_push($a,$row);
}
return $a;
}else{
return false;
}
break;
/**
* Returns a numbered array of the query results

* @property num_results
* @type {Array}
* @public
*/
/**
* Returns a numbered array of the query results

* @property resulsts_num
* @type {Array}
* @public
*/
case 'num_results':case 'results_num':
if($this->query){
$a = array();
$r = $this->results;
while($row = $r->fetch_num()){
array_push($a,$row);
}
return $a;
}else{
return false;
}
break;
case 'assoc_result':case 'result_assoc':
if($this->query){
$r = $this->results;
return $r?$r->fetch_assoc():false;
}else{
return false;
}
break;
case 'num_result':case 'result_num':
if($this->query){
$r = $this->results;
return $r?$r->fetch_num():false;
}else{
return false;
}
break;
case 'insert_id':
return $this->sql->insert_id;
break;
}
}
}
function make_referenced(&$arr){
$refs = array();
foreach($arr as $key => $value){
$refs[$key] = &$arr[$key];
}
return $refs;
}
?>

--- End code ---

Sorunome:
I just stumbled upon this and it is really sweet: https://xerographer.github.io/multicoloure/

Deep Toaster:
While we're on the topic of fonts...

I stumbled upon Fira Code recently. It blew my mind.

Basically, it uses ligatures (a feature whereby two adjacent characters may be displayed differently from the two characters by themselves) to render some common multiple-character programming operators in really neat ways. It's especially useful for Haskell, but it works great in other languages too, for example by combining things like != and >= into ≠ and ≥.

A picture is worth a thousand words.

Sorunome:
Wow, that's a pretty neat idea! I can easily imagine that it takes quite some time to get used to it, though.

Also, is it so that when you backspace both characters dis-appear then?

Deep Toaster:
Nope, the file itself still has the two characters separately—it just shows up as a combined character, taking up twice the space. So pressing Backspace on ≠ gives you !.

It doesn't really take any getting used to, since the characters are all still there, just displayed slightly differently. If anything it's made my code more intuitive because I can see where one operator ends and another begins :D

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version