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 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');

?>

Roman Conversion

<?php

$lines = file('http://hyperspatial.com/romans.txt');
//$lines = array('MM','IX','X');
$array_of_converted_values = array();

foreach($lines as $line_num => $line) $array_of_converted_values[] = convert_roman_numeral(htmlspecialchars($line));

print_r($array_of_converted_values);

function convert_roman_numeral($roman_numeral = 'M'){
$roman_array = array('M' => 1000,'CM' => 900,'D' => 500,'CD' => 400,'C' => 100,'XC' => 90,'L' => 50,'XL' => 40,'X' => 10,'IX' => 9,'V' => 5,'IV' => 4,'I' => 1);
$result = 0;
foreach($roman_array as $key => $value) {
while(strpos($roman_numeral, $key) === 0) {
$result += $value;
$roman_numeral = substr($roman_numeral, strlen($key));
}
}
return $result;
}
?>

Parse Hyperlink

<?php
$line = '<a href="/users/adam">Edit profile</a>';
$converted_line = preg_replace('/<a[^>]*?href=['"](.*?)['"][^>]*?>(.*?)</a>/si','<a href="http://hyperspatial.com$1" target="_blank">$2</a>',$line);
echo $converted_line;
?>

List directories

<?php

foreach(glob($_SERVER['DOCUMENT_ROOT'] . '/pathtoparentdir/*', GLOB_ONLYDIR) as $dir){
$dir = basename($dir);
echo $dir . '<br>';
}

?>

Zip Directory Recursively

<?php

$file = tempnam("tmp","zip");
zip_directory('/home/mydirectorypath',$file);

header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="myzip.zip"');

readfile($file);
unlink($file);

function zip_directory($source,$tempfile){
if(!extension_loaded('zip') || !file_exists($source)) return false;
$zip = new ZipArchive();
if(!$zip->open($tempfile,ZIPARCHIVE::CREATE)) return false;
$source = str_replace('\','/',realpath($source));
if(is_dir($source) === true){
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach($files as $file){
$file = str_replace('\', '/', realpath($file));
if(is_dir($file) === true) $zip->addEmptyDir(str_replace($source . '/','', $file . '/'));
else if(is_file($file) === true) $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
elseif(is_file($source) === true) $zip->addFromString(basename($source), file_get_contents($source));
return $zip->close();
}

?>