Skip to content

Instantly share code, notes, and snippets.

View r37r0m0d3l's full-sized avatar

Anton Trofymenko r37r0m0d3l

View GitHub Profile
@r37r0m0d3l
r37r0m0d3l / singleton.php
Created September 12, 2012 12:27
Singleton and adapter pattern
<?php
class singleton
{
public $var;
public function __construct()
{
$this->var = false;
}
@r37r0m0d3l
r37r0m0d3l / headers.php
Created September 12, 2012 12:28
Send headers
<?php
function headers_send($type, $charset = 'utf-8')
{
switch ($type)
{
case 'txt':
case 'text':
case 'plain':
header('Content-type: text/plain; charset='.$charset.'');
break;
@r37r0m0d3l
r37r0m0d3l / var_dump.php
Created September 12, 2012 12:30
Dump variable
<?php
function dump_var($var, $tab = 1)
{
if (is_null($var))
return 'null';
if (is_numeric($var))
return $var;
if (is_string($var))
return "'".$var."'";
if (is_array($var))
@r37r0m0d3l
r37r0m0d3l / itworks.php
Created September 12, 2012 12:59
it works
<?php
class _
{
/**
* @param string $name
* @return null|mixed
*/
function __get($name)
{
return isset($_REQUEST[$name]) ? $_REQUEST[$name] : null;
@r37r0m0d3l
r37r0m0d3l / object_to_array.php
Created September 26, 2012 09:39
Object to array
<?php
function object_to_array($obj)
{
$arr = array();
foreach (($obj = is_object($obj) ? get_object_vars($obj) : $obj) as $key=> $val)
$arr[$key] = (is_array($val) or is_object($val)) ? $this->object_to_array($val) : $val;
return $arr;
}
@r37r0m0d3l
r37r0m0d3l / gravatar.php
Created October 2, 2012 16:46
Gravatar
<img src="http://www.gravatar.com/avatar/<?=md5($email_address)?>?s=32" width="32" height="32">
@r37r0m0d3l
r37r0m0d3l / is_includable.php
Created October 5, 2012 07:51
Is includable
<?php
function is_includable($path)
{
return file_exists($path) and is_readable($path);
}
@r37r0m0d3l
r37r0m0d3l / phone_format.php
Created October 9, 2012 13:08
Phone format
<?php
function phone_format($phone)
{
$phone = preg_replace('/[^0-9]/', '', $phone);
switch(strlen($phone))
{
case 6:
return preg_replace('/([0-9]{2})([0-9]{2})([0-9]{2})/', '$1-$2-$3', $phone);
break;
case 7:
@r37r0m0d3l
r37r0m0d3l / javascript_oop_examples.js
Created November 6, 2012 11:58
JavaScript OOP Examples
// ---------------
// Dynamic objects
// ---------------
/**
* Creates object on the fly (literal version)
* @type {Object}
*/
var DynamicObject = {
/**
@r37r0m0d3l
r37r0m0d3l / dataset from event.js
Created November 15, 2012 11:08
javascript get dataset from event
// as property
event.currentTarget.dataset.id
// as array element
event.currentTarget.dataset['id']
// jquery method
$(event.currentTarget).data('id')