|
|
|
| Title : |
Inline Scope Control - The inline class provides methods for creating and destroying local variable scopes. Simply put, local scopes are spaces where some or all of the global variables are temporarily hidden. |
| Categories : |
PHP, PHP Classes, Variables |
 Zak Greant |
| Date : |
Feb 14th 2001 |
| Grade : |
2 of 5 (graded 5 times) |
| Viewed : |
5348 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Zak Greant |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?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. |
+----------------------------------------------------------------------+
*/
?> |
|
| A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms, Variables, Arrays, PHP, PHP Classes | | | Array Insertion Categories : PHP, PHP Classes, Arrays | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | | | Pageinfo: Array containing page URI, page query string (parameters), request method (GET or POST) and the complete URI Categories : Variables, PHP Options and Info, Arrays, URLs, PHP | | | Global Dump Highlighted Categories : PHP, Variables, Global Variables | | | Customizable Calendar Class Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar | | | Simple and fast user authentication Categories : PHP, PHP Classes, Authentication | | | Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a Categories : HTML, PHP, PHP Classes, Regexps | | | HTML_Graphs provides a simple PHP interface for
creating pure HTML charts. Categories : Graphics, PHP, PHP Classes, Charts and Graphs | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Bs_IniHandler is a class that can read and write ini-style files (and strings) Categories : PHP, Filesystem, PHP Classes | | | HTTP gzip compression Categories : PHP, PHP Classes, Compression, Zlib | | | phpFormGenerator for Dynamic Form Generation from MySQL Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP | | | MySQL Class to ease Database connectivity Categories : MySQL, PHP Classes, Databases, PHP | | | DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser! Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP | |
| |
| |
|