Location: PHPKode > scripts > Data Registry > Data_Registry_Examples.php
<?php
	date_default_timezone_set("Europe/Madrid");
	
	// Define testDR for example purpuoses
	$testClass = "
class testDR
{
	var \$startDate = '';
	var \$_data = array();
	
	function __construct()
	{
		\$this->startDate = date('Y-m-d H:i:s');
	}
	function doTest()
	{
		echo 'An example of class load. Started at '.\$this->startDate;
	}
	function setData(\$var,\$value)
	{
		\$this->_data[\$var] = \$value;
	}
	function getData(\$var,\$default=FALSE)
	{
		if (isset(\$this->_data[\$var]))
			return \$this->_data[\$var];
		else
			return \$default;
	}	
}";
	// Loading class EVER BEFORE session_start()
	// If a class is saved in persistence mode, must to be declared BEFORE the session start (load session content)
	// Other possibilitie, it's to have an autoload declared, in this case, the class will be loaded automaticly
	eval ($testClass);
	

include "Data_Registry.php";

session_start();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>Data Registry Basic Persistence Examples</title>
	<meta name="description" content="Basic persistence examples of Data Registry Class" />
	<meta name="keywords" content="php5, poo, data registry, data_registry, persistent data, datastorage, data storage"/>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="Content-Language" content="ens" />
	<script tye="text/javascript" src="jquery-min.js"></script>
	<script tye="text/javascript">
		function showDiv(item)
		{
			$('.dexx').each(function(){
				if($(this).attr('id') != item)
				{
					$(this).slideUp();
				}
			});
			$('#'+item).slideToggle();
		}
	</script>
</head>
<body>
	<div style="text-align:center;">
			<h1>Data Registry Basic Persistence Example2</h1>
	<?php 
//		Data_Registry::init()->clearAllNameSpaces();exit;
		$VarName = "ExampleVar";
		$VarArray = "ExampleArray";
		$VarArray2 = "ExampleIncrementArray";
		$NameSpace = "DEFAULT";
		
		// Setting persistence enabled for all namespaces

		Data_Registry::init($NameSpace)->setPersistence(TRUE);

		if (isset($_REQUEST["NAMESPACE"]))
		{
			$NameSpace = $_REQUEST["NAMESPACE"];
			Data_Registry::init($NameSpace)->setPersistence(TRUE);
		}

		// Restoring all namespaces saved previously that exists in SESSION
		Data_Registry::init()->setStorageType('Files',"/tmp/DRFiles");
		echo "Files are saved in /tmp/DRFiles <- This directory must to be created to work properlly.";
		Data_Registry::init()->restoreNameSpace(TRUE);


//var_dump(Data_Registry::init('FILE:Data_Registry.php')->loadNameSpaceFile("c:/temp/DRF")->get("FILE"));
//Data_Registry::init('FILE:Data_Registry.php')->set("FILE",file_get_contents('Data_Registry.php'))->saveNameSpaceFile("c:/temp/DRF","FILE-");
//		
		// Setting some example values
		Data_Registry::init('DEFAULT')->set("Num tests done",Data_Registry::init('DEFAULT')->get("Num tests done")+1);
		Data_Registry::init('DEFAULT')->set("Num of vars in registry",count(Data_Registry::init('DEFAULT')->listVars()));
				
		if (isset($_REQUEST["VARNAME"]))
			$VarName = $_REQUEST["VARNAME"];
		if (isset($_REQUEST["VARARRAY"]) && isset($_REQUEST["NAME"]) && isset($_REQUEST["SURNAME"]))
			Data_Registry::init($NameSpace)->setKey($_REQUEST["VARARRAY"],"Name",$_REQUEST["NAME"])->setKey($_REQUEST["VARARRAY"],"SurName",$_REQUEST["SURNAME"]);
		if (isset($_REQUEST["VARARRAY2"]) && isset($_REQUEST["NAME"]) && isset($_REQUEST["SURNAME"]) && isset($_REQUEST["ARRAYADD"]))
			Data_Registry::init($NameSpace)->add($_REQUEST["VARARRAY2"],array("Name" => $_REQUEST["NAME"],"SurName" => $_REQUEST["SURNAME"]));
					
		if (isset($_REQUEST["CLEAR"]))
		{
			if ($_REQUEST["CLEAR"]=='ALL')
				Data_Registry::init($NameSpace)->clearNameSpace();
			else
				Data_Registry::init($NameSpace)->clear($_REQUEST["CLEAR"]);
		}	
		
		if (isset($_REQUEST["SETCLASS"]))
		{
			Data_registry::init($NameSpace)->getClass($_REQUEST["SETCLASS"]);
			if (isset($_REQUEST["OBJVAR"]) && isset($_REQUEST["OBJVALUE"]))
			{
				Data_registry::init($NameSpace)->getClass($_REQUEST["SETCLASS"])->setData($_REQUEST["OBJVAR"],$_REQUEST["OBJVALUE"]);
			}
		}
		
		if (isset($_REQUEST["VALUE"]))
		{
			$VarValue = $_REQUEST["VALUE"];
			Data_Registry::init($NameSpace)->set($VarName,$VarValue);
		}
		if (isset($_REQUEST["VARKEY"]))
			$VarName = $_REQUEST["VARKEY"];
			
		if (!Data_Registry::init('DEFAULT')->test($VarName))
			Data_Registry::init('DEFAULT')->set($VarName,"CHANGE ME TO SEE HOW IT WORKS");
		
		if (!Data_Registry::init('DEFAULT')->test($VarArray))
			Data_Registry::init('DEFAULT')->set($VarArray,array('Name' => 'Bill','SurName' => 'Smith'));
			?>
		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex1');">Current Persistent Data</a></h2>
		</div>
		<div class="dexx" id="dex1" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<table align="center" border="1" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99"><b>Namespace: </b><?php echo $NameSpace?></td>
			</tr>
			<tr>
				<td width="500"><b>Var Info</b></td>
				<td width="500"><b>Var Content</b></td>
				<td width="20"><b>Clear</b></td>
			</tr>
			<?php foreach (Data_Registry::init($NameSpace)->listVars() as $key => $val) :?>
			<tr>
				<td align="left"><?php echo str_replace("\n","<br>",print_r($val,true))?></td>
				<td align="left"><?php echo str_replace("\n","<br>",print_r(Data_Registry::init($NameSpace)->get($val["varname"]),true))?></td>
				<td width="20"><input type="button" value="X" style="color:red;font-weight:bold;" onClick="document.location.href='<?php echo $_SERVER["PHP_SELF"]."?NAMESPACE=".$NameSpace."&CLEAR=".$val["varname"]?>';"></input></td>
			</tr>
			<?php endforeach;?>
			</table>
			<br />
			<input type="button" value="Plain reload" onClick="document.location.href='<?php echo $_SERVER["PHP_SELF"]?>?NAMESPACE=<?php echo $NameSpace?>';"></input>
			<br />
			<br />
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">Here are the list of all vars saved in current namespace. When you press 'Palin reload', current page are reloaded only with Namespace param, all vars are retrived from storage and can be accessed again.</td>
			</tr>
			</table>
		<br />
		<br />
		</div>

		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex2');">Set or change data</a></h2>
		</div>
		<div class="dexx" id="dex2" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setData" action="" method="GET">
			<input type="hidden" name="NAMESPACE" value="<?php echo $NameSpace?>"></input>
			<table border="0" cellspacing="1" cellpadding="1">
				<tr>
					<th valign="top">VarName</td>
					<th valign="top">Value</th>
				</tr>
				<tr>
					<td width="200" valign="top"><input name="VARNAME" value="<?php echo $VarName?>"></input></td>
					<td width="500" valign="top"><textarea name="VALUE" rows="4" cols="60"><?php echo Data_Registry::init($NameSpace)->get($VarName)?></textarea></td>
				</tr>
				<tr><td>&nbsp;</td></tr>
				<tr><td colspan="2"><input type="submit" value="Submit"></input></td></tr>
			</table>
			</form>
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">Try to change default values or varnames with diferent values, When 'submit' button is pressed, vars are saved in it's storage.</td>
			</tr>
			</table>
			<br />
			<br />
		</div>

		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex3');">Initizalize a Class</a></h2>
		</div>
		<div class="dexx" id="dex3" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setData" action="" method="GET">
			<input type="hidden" name="NAMESPACE" value="<?php echo $NameSpace?>"></input>
			<table border="0" cellspacing="1" cellpadding="1">
				<tr>
					<th valign="top">ClassName</td>
					<th valign="top">ClassDefinition</th>
				</tr>
				<tr>
					<td width="200" valign="top"><input type="hidden" name="SETCLASS" value="testDR">testDR</input></td>
					<td valign="top" align="left"><?php echo str_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",str_replace("\n","<br>",$testClass));?></td>
				</td>
				</tr>
				<tr><td>&nbsp;</td></tr>
				<tr><td colspan="2"><input type="submit" value="Submit"></input></td></tr>
				<tr><td>&nbsp;</td></tr>
				<tr><td>&nbsp;</td></tr>
				<tr>
					<td colspan="2" align="left">
					<?php 
						if (Data_Registry::init($NameSpace)->test('testDR'))
						{ 
							echo "<ul><li>Class testDR is LOADED<br />";
							echo "<li>Now, testing doTest method: <br />&nbsp;&nbsp;&nbsp;<i>Data_Registry::init('$NameSpace')->getClass('testDR')->doTest() : </i><b><br /> ";
							echo "<br />".Data_Registry::init($NameSpace)->getClass('testDR')->doTest();
							echo "</b> <br />";
							echo "<li>Dump Class: <br />&nbsp;&nbsp;&nbsp;<i>var_dump(Data_Registry::init('$NameSpace')->getClass('testDR'))</i>";
							echo "<br />";
							var_dump(Data_Registry::init($NameSpace)->getClass('testDR'));
							echo "<br /></ul>";
						}
						else
						{
							echo "<ul><li>Class testDR NOT LOADED YET<br />";
							echo "<br /></ul>";
						}
						?>
					</td>
				</tr>
			</table>
			</form>
			<br />
			<br />
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">You can save a class and would be accessed at any moment during current session, not needed to be contructed again. Here are a simple class, and you can see it's capabilities.</td>
			</tr>
			</table>
			<br />
			<br />
		</div>

		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex4');">Reuse a class to get methods</a></h2>
		</div>
		<div class="dexx" id="dex4" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setData" action="" method="GET">
			<input type="hidden" name="NAMESPACE" value="<?php echo $NameSpace?>"></input>
			<input type="hidden" name="SETCLASS" value="testDR">
			<table border="0" cellspacing="1" cellpadding="1">
				<tr>
					<th valign="top">Var Name</td>
					<th valign="top">Value</th>
				</tr>
				<tr>
					<td width="200" valign="top"><input name="OBJVAR" value=""></input></td>
					<td width="300" valign="top"><input name="OBJVALUE" value=""></input></td>
				</tr>
				<tr><td>&nbsp;</td></tr>
				
				<tr><td colspan="2"><input type="submit" value="Submit"></input></td></tr>
				<tr><td>&nbsp;</td></tr>
				<tr><td>&nbsp;</td></tr>
			</table>
			</form>
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">A class is constructed if not started before. Here an example ussing directly a class method:
				<br />
				<br />
				<i>Data_registry::init("<?php echo $NameSpace?>")->getClass("testDR")->setData("VarName","Value")</i>
				<br />
				or use:
				<br />
				<i>Data_registry::init("<?php echo $NameSpace?>","testDR")->setData("VarName","Value")</i>
				</td>
			</tr>
			</table>
			<br />
			<br />
		</div>

		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex5');">Set or change array</a></h2>
		</div>
		<div class="dexx" id="dex5" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setData" action="" method="GET">
			<input type="hidden" name="NAMESPACE" value="<?php echo $NameSpace?>"></input>
			<table border="0" cellspacing="1" cellpadding="1">
				<tr>
					<th valign="top">VarArray</td>
					<th valign="top">Name</th>
					<th valign="top">Surname</th>
				</tr>
				<tr>
					<td width="200" valign="top"><input name="VARARRAY" value="<?php echo $VarArray?>"></input></td>
					<td width="300" valign="top"><input name="NAME" value="<?php echo Data_Registry::init($NameSpace)->getKey($VarArray,'Name')?>"></input></td>
					<td width="300" valign="top"><input name="SURNAME" value="<?php echo Data_Registry::init($NameSpace)->getKey($VarArray,'SurName')?>"></input></td>
				</tr>
				<tr><td>&nbsp;</td></tr>
				<tr><td colspan="2"><input type="submit" value="Submit"></input></td></tr>
			</table>
			</form>
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">Any type of data can be stored, here an array example.</td>
			</tr>
			</table>
			<br />
			<br />
		</div>
			
		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex6');">Add elements to an array</a></h2>
		</div>
		<div class="dexx" id="dex6" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setData" action="" method="GET">
			<input type="hidden" name="NAMESPACE" value="<?php echo $NameSpace?>"></input>
			<input type="hidden" name="ARRAYADD" value="1"></input>
			<table border="0" cellspacing="1" cellpadding="1">
				<tr>
					<th valign="top">VarArray</td>
					<th valign="top">Name</th>
					<th valign="top">Surname</th>
				</tr>
				<tr>
					<td width="250" valign="top"><input name="VARARRAY2" size="30" value="<?php echo $VarArray2?>"></input></td>
					<td width="200" valign="top"><input name="NAME" size="40" value=""></input></td>
					<td width="200" valign="top"><input name="SURNAME" size="40" value=""></input></td>
				</tr>
				<tr><td>&nbsp;</td></tr>
				<tr><td colspan="2"><input type="submit" value="Add"></input></td></tr>
			</table>
			</form>
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">Add function, adding elements to an stored array. Very usefull if you plan to use Data_registry to store queries, execution times, errors, logs, etc... You will be able to retrieve all of then at any moment, not only in the current process.</td>
			</tr>
			</table>
			<br />
			<br />
		</div>

		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex7');">Change or create NameSpace</a></h2>
		</div>
		<div class="dexx" id="dex7" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setNameSpace" action="" method="GET">
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td width="200"><b>Change/create NameSpace</b></td>
				<td width="200" valign="top"><input name="NAMESPACE" size="40" value="<?php echo $NameSpace?>"></input></td>
			</tr>
			<tr><td>&nbsp;</td></tr>
			<tr><td colspan="2"><input type="submit" value="Submit"></input></td></tr>
			<tr>
			</tr>
			</table>
			</form>
			<br />
			<br />
			<form name="setNameSpace3" action="" method="GET">
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td width="200"><b>UsedNameSpaces</b></td>
				<td width="200" valign="top">
					<select name="NAMESPACE">
					<?php foreach (Data_Registry::init()->getUsedNameSpaces() as $key => $val) : ?>
						<option value="<?php echo $val?>" <?php if ($NameSpace == $val) echo "SELECTED"?>><?php echo $val?></option>
					<?php endforeach;?>
					</select>
				</td>
			</tr>
			<tr><td>&nbsp;</td></tr>
			<tr>
				<td colspan="99"><input type="button" value="Change" onClick="document.location.href='<?php echo $_SERVER["PHP_SELF"]?>?NAMESPACE='+setNameSpace3.NAMESPACE.value;"></input></td>
			</tr>
			</table>
			</form>
			<br />
			<br />
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">You can create a new namespace, if exists, current namespace are changed by the one entered, if not, new namespace are created.</td>
			</tr>
			</table>
		</div>		

		<div style="margin-left:100px;background-color: #f0f0f0; width:800px;;text-align:left;">
			<h2><a href="Javascript:;" onclick="showDiv('dex8');">Clear NameSpace</a></h2>
		</div>
		<div class="dexx" id="dex8" style="display:none;margin-left:100px;background-color: #f0f0f0; width:800px;">
			<form name="setNameSpace" action="" method="GET">
			<input type="hidden" name="CLEAR" value="ALL"></input>
			<table align="center" border="0" cellspacing="1" cellpadding="1">
				<td width="200"><b>NameSpace to clear</b></td>
				<td width="200" valign="top">
					<select name="NAMESPACE">
					<?php foreach (Data_Registry::init()->getUsedNameSpaces() as $key => $val) : ?>
						<option value="<?php echo $val?>" <?php if ($NameSpace == $val) echo "SELECTED"?>><?php echo $val?></option>
					<?php endforeach;?>
					</select>
				</td>
			</tr>
			<tr><td>&nbsp;</td></tr>
			<tr><td colspan="2"><input type="submit" value="Clear"></input></td></tr>
			</table>
			</form>
			<br />
			<br />
			<table align="center" border="0" cellspacing="1" cellpadding="1">
			<tr>
				<td colspan="99">If you don't need data anymore, you can clear a namespace.</td>
			</tr>
			</table>
		</div>		
		<br />
		<br />

	</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-111018-9");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
Return current item: Data Registry