<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.
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.
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
$reg = new registry();
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
class registry{
public $reg;
public function __construct($reg = null){
$this->reg = array();
}
public function isValid($key){
return array_key_exists($key, $this->reg);
}
public function set($key, $obj){
if(!$this->isValid($key)){
$this->reg[$key] = $obj;
$retVal = true;
}else{
$retVal = false;
}
return $retVal;
}
public function get($key){
if($this->isValid($key)){
$retVal = $this->reg[$key];
}else{
$retVal = false;
}
return $retVal;
}
public function overWrite($key, $obj){
if($this->isValid($key)){
$this->reg[$key] = $obj;
$retVal = true;
}else{
$this->reg[$key] = $obj;
$retVal = false;
}
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..
@require_once('/path/to/registry.php');
$reg = new registry();
$reg2 = new registry();
$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.