Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Dec 2002
Posts: 3,255
Likes: 3
UGN Elite
OP Offline
UGN Elite
Joined: Dec 2002
Posts: 3,255
Likes: 3
<rant>

Okay so it has been a while since I was up in this biotch. I have been busy. Having developed in PHP for the last 8 years I have evolved with it. Started out coding as most in procedural style. In the last 3 years I have done almost nothing but OOP code. PHP5 was a huge step forward in terms of OOP capabilities. In my opinion since PHP has stopped development on PHP4 it is best to move up to PHP5 as if you do not PHP6 will be a real pain for you.

</rant>

Okay so how many of you have ran into this problem. You design a function.
PHP Code


function myFunc(){
$value = 'This is my value';
}

echo
$value;


Only to find out the example above no worky. This can be solved with a return clause.

PHP Code


function myFunc(){
$value = 'This is my value';
return
$value;
}

echo
myFunc();


Now that works... But what if we want to have a function give us back more than one value. We could creat an array in the function and pass that back. Then break the array down after we call the function. Bleh. Work work work. Code code code.

What if we had one place to store values. A magical place that crosses name space, yet helps us not overwrite our values on accident. Kinda like a registry of sorts. Well I have that place. Here is how it works.

PHP Code



<?php
/**
* Declare my registry
*/
$reg = new registry();

/**
* Define my function
*/
function myFunc( $reg ){
$reg->set('value', 'This is my value');
$reg->set('value2', 'This is my other value');
$reg->set('value3', 'This is my third value')

$myArray = array();
$myArray[1] = 'string 1';
$myArray[2] = 'string 2';
$myArray[3] = 'string 3';

$reg->set('myArray', $myArray);
}

echo
$reg->get('value');

echo
$reg->get('value2');

echo
$reg->get('value3');

print_r(
$reg->get('myArray')
);


The above code, works if you use the registry pattern. Instead of having to pass around tons of vars in your code, you could pass just one. Here is the code for my registry class.

PHP Code



<?php
/**
* Registry class file
*
* This is the CMS registry. All global objects should be registered in this class before use.
* @author Russell Vance
* @copyright (C)VanceConcepts.com 2007
* @version 0.0.1
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @filesource
* @todo Look into implementing multiple registries. For example maybe a seperate one for MySQL or Smarty
*/


/**
* This is the registry class for the system.
*
* The registry allows for a centralized place to store objects and data. Any class can store and retrieve objects from here. Uses an implementation of the registry pattern.
* @author §intå×
* @copyright (C)VanceConcepts.com 2007
* @version 0.0.3
* @Package Registry
* @since 5/28/2008
*/

class registry{
/**
* This variable will store the registry in an array. It can not be called directly, you must used the get() and set() methods.
* @access private
* @var array
*/
public $reg;

/**
* This is the constructor for the registry class
*
* This method will initiate the class. In other words to use this method you MUST get an instance of this class.
* To initiate this class you need to something like the following example.
* <code>
* //Example:
* $_reg = new registry(); # No parameters are required for start up.
* </code>
* @access public
* @param void
* @return void
*/
public function __construct($reg = null){
$this->reg = array();
}

/**
* This method checks to see if a object is stored in the registry or not.
*
* This is a private method and is not called directly. It Checks the registry
* to see is a registry key exists. It returns the coresponing boolean value.
* @access public
* @param string $key The key to the object you are trying to retreive.
* @return boolean [True] if registry key exists, [False] is it doesn't.
*/
public function isValid($key){
return
array_key_exists($key, $this->reg);
}

/**
* This method creats new registry items.
*
* This is method takes a key name and a object and creates a new registry
* item. The key name should have no spaces. If the key Name exists already
* a catchable exception is thrown.
* <code>
* Example:
*
* $reg = new registry();
* try{
* $reg->set( 'mysqli', new mysqli( $host:$port, $user, $psswd, $database ) );
* }catch(Exception $e){
* controlCore::handleError($e->getMessage(), 'general');
* }
* </code>
* @uses registry::isValid() Used to test if a key is valid. isValid is a private method and can not be called directly.
* @access public
* @param string $key The key to the object you are trying to retreive.
* @param object $obj The object you want stored in the registry
* @return boolean [True] if new registry item is created, [False] if registry item exists already.
*/
public function set($key, $obj){
if(!
$this->isValid($key)){
$this->reg[$key] = $obj;
$retVal = true;
}else{
$retVal = false;
//controlCore::handleError('Could not set registry item "'.$key.'" as it is in use already', 'reg');
}
return
$retVal;
}

/**
* This method retrieves an object from the registry
*
* This is the accesor method for the Registry. It requires you call it with the
* key to the object you want to retrieve. On failure a catchable exception is thrown.
* <code>
* Example:
*
* $reg = new registry();
*
* //Insert object into registry
* try{
* reg->set( 'mysqli', new mysqli( $host:$port, $user, $psswd, $database ) );
* }catch(Exception $e){
* controlCore::handleError($e->getMessage(), 'general');
* }
*
* //get object from registry
* try{
* $mysqli = reg->get('mysqli');
* }catch(Exception $e){
* controlCore::handleError($e->getMessage(), 'general');
* }
* </code>
* @access public
* @uses registry::isValid() Used to test if a key is valid. isValid is a private method and can not be called directly.
* @param string $key The key to the object you are trying to retreive.
* @return object|boolean On sucess an object is returned, On fail a boolean value of false is returned.
*/
public function get($key){
if(
$this->isValid($key)){
$retVal = $this->reg[$key];
}else{
$retVal = false;
//controlCore::handleError('Could not get registry item "'.$key.'." Does not exist in registry','reg');
}
return
$retVal;
}

/**
* This method over writes an object in the registry
*
* This is the over write method for the Registry. It requires you call it with the
* key to the object you want to over write and the data you are over writing
* with. If the key did not exist previously, a boolean false is returned and an
* exception is thrown, but the data is entered into the database.
* <code>
* Example:
*
* $reg = new registry();
*
* //Insert object into registry
* try{
* $reg->set( 'mysqli', new mysqli( $host:$port, $user, $psswd, $database ) );
* }catch(Exception $e){
* controlCore::handleError($e->getMessage());
* }
*
* //Over write object in the registry
* try{
* $reg->overWrite('mysqli', new mysqli( $alt_host:$alt_port, $alt_user, $alt_psswd, $alt_database ) );
* }catch(Exception $e){
* controlCore::handleError($e->getMessage());
* }
* </code>
* @access public
* @uses registry::isValid() Used to test if a key is valid. isValid is a private method and can not be called directly.
* @param string $key The key to the object you are trying to retreive.
* @param mixed $obj Any data that you want to store. Arrays, objecsts, boolean, strings, integers etc.
* @return boolean On boolean [TRUE] is returned, On fail a boolean value of [FALSE] is returned and a catchable exception is thrown.
*/
public function overWrite($key, $obj){
if(
$this->isValid($key)){
$this->reg[$key] = $obj;
$retVal = true;
}else{
$this->reg[$key] = $obj;
$retVal = false;
//controlCore::handleError('Could not over write registry item because it does not exist"'.
// $key.'." Did not exist in registry', 'reg');
}
return
$retVal;
}
}
?>


You will notive in the code "controlCore::handleError" I have another class I will be sharing named controlCore. For now I have commented it out. If you wanted to handle errors however, this is where your code would go. Feel free to use this class, change it, re-write it. I really do not care it is nothing major. It is however a nice easy class to start teaching with.

In this class we have

  • 1 class variable named $reg
  • 5 methods
    • __construct() - instantiates the class
    • isValid() - Checks to make sure a value exists
    • get() - retrieves a value from the register
    • set() - Sets a value into the registry, will not let you overwrite a value.
    • overWrite() - allows for explicit overwrite of a value.


To use, copy and save the code to registry.php(or whatever you want to name it).

In your code you must require or include the code once..

PHP Code


@require_once('/path/to/registry.php');

//Instantiate the class
$reg = new registry();

//If you wanted more than one just do this.
$reg2 = new registry();

//To use methods just use the ->

$reg->set('value1', 'some Value');

echo
$reg->get('value1');

$reg->overWrite('value1', 'A new Value');

echo
$reg->get('value1');



I make you call overWrite() explicitly to prevent accidental overwrites. Each method calls isValid() before doing what you ask it to do. If a value already exists, set() will fail and return false. Please post any questions below. I hope it helps but I do not guarantee this code to work, use it at your own risk. I can not know every set up of every system. Please test well before putting this in a production environment. If you make improvements to the code feel free to share. I can not find it but I have a version of this with a flush method. I had a project where I actually needed to be able to flush the whole registry and be able to rebuild it. I will post that later.

Joined: Dec 2002
Posts: 3,255
Likes: 3
UGN Elite
OP Offline
UGN Elite
Joined: Dec 2002
Posts: 3,255
Likes: 3
I will be documenting this with phpDocumentor and making the documentation available too. Next up is php5.2.x OOP and caching mysql result sets.

Last edited by §intå×; 05/30/08 03:41 PM.
Joined: Feb 2002
Posts: 7,203
Likes: 11
Community Owner
Offline
Community Owner
Joined: Feb 2002
Posts: 7,203
Likes: 11
I need to get the code parser updated lol


Donate to UGN Security here.
UGN Security, Back of the Web, and VNC Web Services Owner
Joined: Dec 2002
Posts: 3,255
Likes: 3
UGN Elite
OP Offline
UGN Elite
Joined: Dec 2002
Posts: 3,255
Likes: 3
Just get the black text to be white and I think we are good.

Joined: Dec 2002
Posts: 3,255
Likes: 3
UGN Elite
OP Offline
UGN Elite
Joined: Dec 2002
Posts: 3,255
Likes: 3
That is why you are the man Gizmo. looks MUCH better man. Nice.


Link Copied to Clipboard
Member Spotlight
Posts: 43
Joined: November 2002
Forum Statistics
Forums41
Topics33,840
Posts68,858
Average Daily Posts1
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Where and how do you torrent?
by danni75 - 03/01/24 05:58 AM
Animation,
by JohanKaariainen - 08/15/19 01:18 AM
Blackbeard.....
by Gremelin - 10/03/18 07:02 PM
my old account still exists!
by Crime - 08/10/18 02:47 PM
Okay WTF?
by HenryMiring - 09/27/17 01:45 AM
The History Thread...
by Gremelin - 08/11/17 12:11 PM
My friend NEEDS your HELP!
by Lena01 - 07/21/17 12:06 AM
I'm having fun with this guy.
by gabithompson730 - 07/20/17 01:50 AM
I want to upgrade my phone
by gabithompson730 - 07/20/17 01:49 AM
Doom 3
by Cyrez - 09/11/14 08:58 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666 - 08/22/14 09:21 AM
AIM scene 99-03
by lavos - 09/02/13 08:06 AM
Planetside 2
by Crime - 03/04/13 07:10 AM
Beta Testers Wanted
by Crime - 03/04/13 06:55 AM
Hello Everyone
by Gremelin - 02/12/12 06:01 PM
Tracfone ESN Generator
by Zanvin Green - 01/18/12 01:31 PM
Python 3 issue
by Testing - 12/17/11 09:28 PM
tracfone airtime
by Drache86 - 07/30/11 03:37 AM
Backdoors and the Infinite
by ZeroCoolStar - 07/10/11 03:52 AM
HackThisZIne #12 Releaseed!
by Pipat2 - 04/28/11 09:20 PM
gang wars? l33t-wars?
by Gremelin - 04/28/11 05:56 AM
Consolidate Forums
by diggin2deep - 04/21/11 10:02 AM
LAN Hacking Noob
by Gremelin - 03/12/11 12:42 AM
Top Posters
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Likes Received
Ghost 2
unreal 1
Crime 1
Ice 1
Dartur 1
Powered by UBB.threads™ PHP Forum Software 7.7.5