Session to object

<?php
//Usage Test
$my_session = new VrSession('my_session');

$my_session->test1 = 'value1'; //comment this line out to test 'unset' below
$my_session->test2 = 'value2';

//unset($my_session->test1);

echo '<pre>';
print_r($my_session);
echo '</pre>';

//Check for test 1
if(isset($my_session->test1)) echo '<br>test1 = ' . $my_session->test1 .'<br>';
else echo '<br>test1 not-set';

/* ~~~~~~~~~~ VrSession ~~~~~~~~~~ */

class VrSession{
private $namespace,$session = array();
public function __construct($namespace){
session_start();
$this->namespace = $namespace;
if($_SESSION[$this->namespace]) $this->session = $_SESSION[$this->namespace];
}
public function __set($name,$value){
$_SESSION[$this->namespace][$name] = $value;
$this->session[$name] = $value;
}
public function __get($name){
if(array_key_exists($name,$this->session)) return $this->session[$name];
else return false;
}
public function __isset($name){ return isset($this->session[$name]); }
public function __unset($name){
unset($_SESSION[$this->namespace][$name]);
unset($this->session[$name]);
}
}
?>

Property overload

<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', '1');

class PropertyOverload{
private $data = array();
public function __set($name,$value){ $this->data[$name] = $value; }
public function __get($name){
if(array_key_exists($name,$this->data)) return $this->data[$name];
$trace = debug_backtrace();
trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'],E_USER_NOTICE);
return null;
}
public function __isset($name){ return isset($this->data[$name]); }
public function __unset($name){ unset($this->data[$name]); }
}

$property_overload = new PropertyOverload;

$property_overload->testprop = 'my test property';
$property_overload->testprop2 = 'my test property2';

echo 'Test: '. $property_overload->testprop;
echo '<br>';

$property_overload->testprop = 'my test property, modified';

echo 'Test: '. $property_overload->testprop;
echo '<br>';

echo 'Test: '. $property_overload->testprop2;
echo '<br>';

echo 'Error: '. $property_overload->testprop3;
?>

//Example using a stdClass instead of an array
class PropertyOverloadOO{
private $data;
public function __construct(){ $this->data = new stdClass(); }
public function __set($name,$value){ $this->data->{$name} = $value; }
public function __get($name){
if(array_key_exists($name,$this->data)) return $this->data->{$name};
$trace = debug_backtrace();
trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'],E_USER_NOTICE);
return null;
}
public function __isset($name){ return isset($this->data->{$name}); }
public function __unset($name){ unset($this->data->{$name}); }
}

Better Truncator

<?php

$longString ="http://google-maps.pace.com?var=ntahohk5eoui564x56oe4u564oa5oeu5aoeu5ao5u5eu56io14eui564oe";
$separator = '/.../';
$separatorlength = strlen($separator) ;
$maxlength = 25 - $separatorlength;
$start = $maxlength / 2 ;//Change to max length to truncate end
$trunc = strlen($longString) - $maxlength;

echo substr_replace($longString, $separator, $start, $trunc);

?>

Better mysqli table creation

<?php
$mysqli = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

if($mysqli->connect_errno) echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
echo $mysqli->host_info . "n";

$query = "CREATE TABLE hwt_tools(
id mediumint(8) NOT NULL AUTO_INCREMENT,
user_id mediumint(8),
category_id mediumint(8),
target_url varchar(255),
favicon_url varchar(255),
desc_text text,
UNIQUE KEY id (id)
);";

//Use this to safely create a table while testing
if(!$mysqli->query($query)) echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;

//Use this to drop existing table and start a new
//if(!$mysqli->query("DROP TABLE IF EXISTS hwt_tools") || !$mysqli->query($query)) echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;

?>

Action type callbacks

<?php //Core Functions

/* Insert Html */
function insert($source_id){
global $inserts;
if($inserts[$source_id]){
foreach($inserts[$source_id] as $callback) call_user_func($callback);
}
}

/* Insert Callback */
function insert_callback($source_id,$callback){
global $inserts;
$inserts[$source_id][] = $callback;
}

?>

<!-- HTML in Header -->
<ul id="aux-nav">
<li>Hello</li>
<li>Aloha</li>
<?php insert('aux-nav') ?>
</ul>

<?php //Callback function in a module or plugin

function add_links(){?>
<li>Hola</li>
<?php
}
insert_callback('aux-nav','add_links');

?>

Improved Shortcode

<?php

/* ~~~~~~~~~~~~ Test Routine ~~~~~~~~~~~~*/

//Default Style
hgm_map();

//Array Style
$test_args = array('width' => 500, 'height' => 300);
hgm_map($test_args);

//Query String Style
hgm_map('width=200&height=100');

//Shortcode = [hgm_map width="600" height="400"]

/* ~~~~~~~~~~~~ Functions ~~~~~~~~~~~~*/

/* Main Function */
function hgm_map($args,$shortcode = false){
if($shortcode) ob_start(); //Halt output if shortcode

//Apply defauilts to argumentns and extract
$defaults = array(
'width' => '600',
'height' => '400'
);
$final_args = wp_parse_args($args,$defaults);
extract($final_args,EXTR_SKIP);
?>

<p>Width Value = <?php echo $width ?></p>
<p>Height Value = <?php echo $height ?></p>

<?php
if($shortcode){
$main_function_output = ob_get_contents();
ob_end_clean();
return $main_function_output;
}
}

/* Shortcode - [hgm_map width="600" height="400"] */
function hgm_shortcode($args){ return hgm_map($args,true); }
add_shortcode('hgm_map','hgm_shortcode');

?>