Miscellaneous
|
|
|
|
<?php /*
+----------------------------------------------------------------------+
| Copyright (c) 2000 J.A.Greant |
| (See end of file for usage notes and licensing information.) |
+----------------------------------------------------------------------+
*/
class inline {
var $globals, $locals;
function inline ()
{
if (func_num_args ())
{
$vars = func_get_args ();
foreach ($vars as $var)
{
$var = trim ($var);
if (isset ($GLOBALS[$var]))
{
$this->globals[$var] = $GLOBALS[$var];
unset ($GLOBALS[$var]);
}
$this->locals[$var] = TRUE;
}
}
else
{
reset ($GLOBALS);
while (list ($key) = each ($GLOBALS))
{
if ($key != 'GLOBALS')
{
$this->globals[$key] = $GLOBALS[$key];
unset ($GLOBALS[$key]);
$this->locals[$key] = TRUE;
}
}
}
}
function get_global ($var)
{
if (isset ($this->globals[$var]))
return $this->globals[$var];
return NULL;
}
function get_globals ()
{
return $this->globals;
}
function export ($var)
{
if ($this->locals[$var])
{
$this->globals[$var] = $GLOBALS[$var];
return TRUE;
}
return FALSE;
}
function export_all ($var)
{
if (is_array ($this->locals))
{
reset ($this->locals);
while (list ($key) = each ($this->locals))
$this->globals[$key] = $GLOBALS[$key];
return TRUE;
}
return FALSE;
}
function exit_scope ()
{
/* Restore the global variables */
reset ($this->globals);
if (is_array ($this->globals))
while (list ($key) = each ($this->globals))
$GLOBALS[$key] = $this->globals[$key];
/* Destroy class variables */
unset ($this->globals);
unset ($this->locals);
unset ($this);
}
} /*
+----------------------------------------------------------------------+
| FILE NAME: inline.pkg |
+----------------------------------------------------------------------+
| Author: J.A.Greant |
| Email : zak@nucleus.com |
| Date : 2000/11/23 |
+----------------------------------------------------------------------+
| The inline class provides a simple method for temporarily entering |
| local scopes for variables. The new scope can be restricted to a |
| few variables or (for debugging) the entire scope. This class is |
| most useful for debugging or for making emergency hacks to code. |
| |
| For example: Pretend that you are dealing with a large block of |
| obfuscated code and need to make it work in a very short amount of |
| time. Rather than worry about possible interactions with existing |
| variables, enter a local scope, do your quick hack and then return |
| the global scope. |
| |
| inline() method / class constructor |
| ----------------------------------- |
| Accepts a list of 0 or more variable names. All specified variable |
| names will be hidden until the exit_scope() method is called. If no |
| variable names are specified, then the entire global scope will be |
| hidden. |
| |
|--> WARNING: Hiding the entire global scope in this fashion is <--|
|--> both wasteful and bad coding style. It should really only be <--|
|--> used for debugging and emergencies. <--|
| |
| USAGE EXAMPLE: |
| // Create a local scope for variables $x, $y and $z |
| $my = new inline ('x', 'y', 'z'); |
| // do whatever you will with $x, $y and $z |
| // Return to global scope |
| $my->exit_scope (); |
| |
| get_global() method |
| ------------------- |
| Return the value of a hidden global variable. |
| |
| USAGE EXAMPLE: |
| // Create a local scope for variables $x, $y and $z |
| $my = new inline ('x', 'y', 'z'); |
| // do whatever you will with $x, $y and $z |
| // Find the global value of $x |
| print $my->get_global ('x'); |
| // Return to global scope |
| $my->exit_scope (); |
| |
| get_globals() method |
| -------------------- |
| Return the value of all hidden global variables. |
| |
| USAGE EXAMPLE: |
| // Create a local scope for variables $x, $y and $z |
| $my = new inline ('x', 'y', 'z'); |
| // do whatever you will with $x, $y and $z |
| // Find the global value of $x |
| print $my->get_globals (); |
| // Return to global scope |
| $my->exit_scope (); |
| |
| export() method |
| --------------- |
| Export a local variable to the global scope. |
| |
| USAGE EXAMPLE: |
| // Create a local scope for variables $x, $y and $z |
| $my = new inline ('x', 'y', 'z'); |
| // do whatever you will with $x, $y and $z |
| // Find the global value of $x |
| print $my->export ('x'); |
| // Return to global scope |
| $my->exit_scope (); |
| |
| export_all() method |
| ------------------- |
| Export all local variables to the global scope. |
| |
| USAGE EXAMPLE: |
| // Create a local scope for variables $x, $y and $z |
| $my = new inline ('x', 'y', 'z'); |
| // do whatever you will with $x, $y and $z |
| // Find the global value of $x |
| print $my->export_all (); |
| // Return to global scope |
| $my->exit_scope (); |
| |
| exit_scope() method |
| ------------- |
| Exit the local scope and destroy the class variables. |
| |
| USAGE EXAMPLE: |
| // Create a local scope for variables $x, $y and $z |
| $my = new inline ('x', 'y', 'z'); |
| // do whatever you will with $x, $y and $z |
| // Find the global value of $x |
| print $my->export ('x'); |
| // Return to global scope |
| $my->exit_scope (); |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
|--> WARNING: Be careful if you nest scopes. Make sure to exit the <--|
|--> scopes in the reverse order that you called them. <--|
+----------------------------------------------------------------------+
| For Example: |
| // You should probably avoid this! :) |
| $x = 100; |
| $mine = new scope (); |
| $x = 10; |
| $yours = new scope (); |
| $x = 1; |
| $mine->exit_scope (); // $x will now equal 100 |
| $yours->exit_scope (); // $x will now equal 10 |
+----------------------------------------------------------------------+
+----------------------------------------------------------------------+
| CVS LOG INFO |
+----------------------------------------------------------------------+
$Log: inline.pkg,v $
Revision 1.2 2000/11/24 09:22:26 zak
Reworked code.
Added documentation and bsd-style license.
Revision 1.1 2000/11/24 08:09:04 zak
Initial commit of inline class
+----------------------------------------------------------------------+
| Copyright (c) 2000 J.A.Greant (zak@nucleus.com) |
| All rights reserved. |
+----------------------------------------------------------------------+
| Redistribution and use in source and binary forms, with or without |
| modification, is permitted provided that the following conditions |
| are met: |
+----------------------------------------------------------------------+
| Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| |
| Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| Neither the name of the author nor the names of any contributors to |
| this software may be used to endorse or promote products derived |
| from this software without specific prior written permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------+
*/ ?>
|
|
|
Usage Example
|
include ('scope.php');
// Set a variable that is in global scope
$x = 10;
print "The value of $x is $x<br />";
print 'Leaving global scope<br />';
// Start a scope called $my where $x is locally scoped
$my = new scope ('x');
print 'Entering $my scope<br />';
// $x is not set in the $my scope
print "The value of $x is $x<br />";
// Set a new value for $x
$x = 102;
// The local value of $x is now set
print "The value of $x is $x<br />";
print 'Leaving $my scope<br />';
// Get the value of $x in global scope
$global_x = $my->get_global('x');
print 'Re-entering global scope<br />';
// Exit $my scope
$my->exit_scope ();
print "The value of $x is $x<br />";
|
|
|
Rate This Script
|
|
|
|