Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Last active December 17, 2015 22:49
Show Gist options
  • Save RalfAlbert/5684779 to your computer and use it in GitHub Desktop.
Save RalfAlbert/5684779 to your computer and use it in GitHub Desktop.
Example files for DataContainer
<?php
class DataContainer
{
public static $data = array();
public function __set( $name, $value ) {
self::$data[$name] = $value;
}
public function __get( $name ) {
if ( isset( self::$data[$name] ) )
return self::$data[$name];
else
return null;
}
}
class Bar extends DataContainer
{
public function hello_foo() {
printf( 'Foo used inside %s: %s<br>', __CLASS__, $this->foo );
}
}
function set_foo( $var ) {
$dc = new DataContainer;
$dc->foo = $var;
}
function print_something() {
$dc = new DataContainer;
$foo = $dc->foo;
print( "Foo inside DataContainer: {$dc->foo}<br> Foo as local variable: {$foo}<br>" );
}
function do_something() {
$dc = new DataContainer;
$foo = &$dc::$data['foo'];
$foo = (int) $foo;
$foo++;
}
set_foo( '1' );
print_something();
do_something();
$dc = new DataContainer();
echo "Foo is now: {$dc->foo}<br>";
$bar = new Bar();
$bar->hello_foo();
<?php
class DataContainer
{
public static $data = array();
public function __set( $name, $value ) {
self::$data[$name] = $value;
}
public function __get( $name ) {
if ( isset( self::$data[$name] ) )
return self::$data[$name];
else
return null;
}
}
function set_foo( $var ) {
$dc = new DataContainer;
$dc->foo = $var;
}
function print_something() {
$dc = new DataContainer;
$foo = $dc->foo;
print( "Foo im DataContainer: {$dc->foo}<br> Foo als lokale Variable: {$foo}<br>" );
}
function do_something() {
$dc = new DataContainer;
$foo = &$dc::$data['foo'];
$foo = (int) $foo;
$foo++;
}
set_foo( '1' );
do_something();
$dc = new DataContainer();
echo "Foo is now: {$dc->foo}";
<?php
/*
* mocking set_transient() and get_transient()
*/
function set_transient( $key, $data, $lifetime ) {
// ignore lifetime
( ! session_id() ) AND session_start();
$_SESSION[$key] = $data;
session_write_close();
return true;
}
function get_transient( $key ) {
( ! session_id() ) AND session_start();
return ( isset( $_SESSION[$key] ) ) ? $_SESSION[$key] : false;
}
class DataContainer
{
public static $data = array();
private static $protected = array();
private static $is_saved = false;
public function __construct() {
if ( empty( self::$data ) && false != ( $transient = get_transient( self::TRANSIENT_KEY ) ) )
self::$data = $transient;
// save the data in a transient when the script end
register_shutdown_function( array( __CLASS__, 'shutdown' ) );
}
public function __destruct() {
self::shutdown();
}
public function __set( $name, $value ) {
if ( ! in_array( $name, self::$protected ) )
self::$data[$name] = $value;
}
public function __get( $name ) {
if ( isset( self::$data[$name] ) )
return self::$data[$name];
else
return null;
}
public static function shutdown() {
// save the data only once (__destruct or shutdown)
if ( true === self::$is_saved )
return false;
// save the transient for one day
set_transient( self::TRANSIENT_KEY, self::$data, DAY_IN_SECONDS );
self::$is_saved = true;
return true;
}
public static function protect( $name ) {
self::$protected[$name] = true;
}
public static function unprotect( $name ) {
if ( isset( self::$protected[$name] ) )
self::$protected[$name] = false;
}
}
$abba = new DataContainer();
$abba->waterloo = 'DaDaaaDa';
$abba->tonight = 'Uhuuuu';
$abba::protect( 'tonight' );
echo '<pre>';
print_r($abba::$data);
echo '</pre><hr>';
function elvis() {
$elvis = new DataContainer();
echo "Elvis sagt Waterloo sei <em>{$elvis->waterloo}</em> <br>";
$yeah = 'Yeah! Yeah! Yeah!';
echo "Elvis setzt Tonight auf <em>{$yeah}</em><br>";
$elvis->tonight = $yeah;
echo "Tonight ist jetzt: <em>{$elvis->tonight}</em><br>";
}
elvis();
echo '<hr>';
class Status_Quo
{
private $sdc = null;
public function __construct() {
$this->sdc = new DataContainer();
}
public function waterloo() {
echo "Status Quou singt Waterloo: <em>{$this->sdc->waterloo}</em><br>";
}
public function tonight( $new_value = "one-two-three" ) {
echo "Status Quo setzt Tonight auf <em>{$new_value}</em><br>";
$this->sdc->tonight = $new_value;
echo "Tonight ist jetzt <em>{$this->sdc->tonight}</em><br>";
}
}
$sq = new Status_Quo();
$sq->waterloo();
$sq->tonight();
echo '<hr>';
class Abba extends DataContainer
{
private $sdc = null;
public function __construct() {
$this->sdc = (object) self::$data;
}
public function waterloo() {
$waterloo = self::$data['waterloo'];
echo "Waterloo, {$waterloo}<br>";
}
public function tonight() {
echo "Tonight, {$this->sdc->tonight}<br>";
}
}
$abba = new Abba();
$abba->waterloo();
$abba->tonight();
<?php
class DataContainer
{
public static $data = array();
public static $protected = array();
public function __set( $name, $value ) {
if ( ! in_array( $name, self::$protected ) )
self::$data[$name] = $value;
}
public function __get( $name ) {
if ( isset( self::$data[$name] ) )
return self::$data[$name];
else
return null;
}
public static function protect( $name ) {
if ( ! in_array( $name, self::$protected ) )
array_push( self::$protected, $name );
}
}
function good_foo() {
$dc = new DataContainer();
$dc->foo = 'protected foo';
$dc->bar = 'unprotected bar';
$dc->protect( 'foo' );
}
function bad_bar() {
$dc = new DataContainer();
$dc->foo = 'hahaha!';
$dc->bar = 'I change everything!';
}
$dc = new DataContainer();
echo "Set <code>foo</code> to <b>protected foo</b> and <code>bar</code> to <b>unprotected bar</b><br>";
good_foo();
echo "Try to overwrite <code>foo</code> and <code>bar</code><br>";
bad_bar();
echo "<code>foo</code> is still <b>{$dc->foo}</b> and <code>bar</code> is now <b>{$dc->bar}</b><br>";
<?php
class DataContainer
{
const SESSION_KEY = 'Example_DataConatiner';
public static $data = array();
public function __construct() {
( ! session_id() ) AND session_start();
if ( isset( $_SESSION[self::SESSION_KEY] ) && ! empty( $_SESSION[self::SESSION_KEY] ) )
self::$data = $_SESSION[self::SESSION_KEY];
register_shutdown_function( array( $this, '__destruct' ) );
}
public function __destruct() {
( ! session_id() ) AND session_start();
$_SESSION[self::SESSION_KEY] = self::$data;
session_write_close();
}
public function __set( $name, $value ) {
self::$data[$name] = $value;
}
public function __get( $name ) {
if ( isset( self::$data[$name] ) )
return self::$data[$name];
else
return null;
}
public static function reset() {
self::$data = array();
unset($_SESSION[self::SESSION_KEY]);
session_write_close();
}
}
$dc = new DataContainer();
$uri = $_SERVER['PHP_SELF'];
if ( null === $dc->session_data ) {
$dc->session_data = 'Value from previous page request';
echo 'This is the first run<br>';
die( "<a href='{$uri}?run=2'>Klick</a>" );
}
$pagerequest = filter_input( INPUT_GET, 'run', FILTER_SANITIZE_NUMBER_INT );
echo "This is the {$pagerequest}nd run with <b>{$dc->session_data}</b> from session-data";
$dc::reset();
<?php
function set_foo( $var ) {
global $foo;
$foo = $var;
}
function print_something() {
global $foo;
print( "Foo is {$foo}<br>" );
}
function do_something() {
global $foo;
$foo = (int) $foo;
$foo++;
}
set_foo( 'Hello Bob!' );
print_something();
do_something();
global $foo;
echo 'Foo as integer: '.$foo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment