Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Polaroid Scope

x00x00 MVP
edited April 2009 in Vanilla 1.0 Help
Just a blue sky idea, that may or may not be useful to prevent variable name collisions between extensions as well as enforcing a naming convention for functions/callbacks.

Some global declarations:
session_start(); $globvar = 'this has not changed...'; $globvar1 = 'this has not changed...'; function AnyFunction(){ echo "\n\nFunction declared outside of extension"; } //safe place $extension_vars=array();

Special include function
function includeInPolaroidScope($extension){// scope // get global vars foreach ($GLOBALS As $name => $value){ eval('global $'.$name.';'); } unset($name,$value); //important $funcs_glob= get_defined_functions();//outside functions include($extension.'/default.php'); // inlcude the extension $vars_in_scope = get_defined_vars();// (in scope) foreach($vars_in_scope As $name => $value){ if(!in_array($name, array('extension','funcs_glob')) && !array_key_exists($name,$GLOBALS)){ // not global $extension_vars[$extension][$name] = $value; } // no need to unset local variables } $funcs_curr= get_defined_functions();// current functions foreach($funcs_curr['user'] As $name){ if(!in_array($name,$funcs_glob["user"])){ //inside functions if(($pos = strpos($name,'_')) ===false && substr($name,0,$pos)!=strtolower($extension)){ die("Invalid function name '$name'. Extension functions/callbacks must be prefixed by name e.g. 'ExtensionName_FunctionName'"); } } } }// end scope

Extension to include:
<?php $localvar = 'this is local...'; $globvar = 'this has changed...'; $_SESSION['hi'] = 'hi'; //super global function MyExtension_AnyFunction(){ echo "\n\nFunction declared inside of extension (must have prefix)"; } echo 'global $globvar='.$globvar.' global $globvar1='.$globvar1.' local $localvar='.$localvar; ?>

Include MyExtension:
//include extension includeInPolaroidScope('MyExtension'); echo "\n\n*Outside scope*"; echo "\n\n\$extension_vars="; var_export($extension_vars);//only relevant echo "\n\n\$localvar="; var_export(@$localvar);//not set MyExtension_AnyFunction(); AnyFunction();

global $globvar=this has changed... global $globvar1=this has not changed... local $localvar=this is local... *Outside scope* $extension_vars=array ( 'MyExtension' => array ( 'localvar' => 'this is local...', ), ) $localvar=NULL Function declared inside of extension (must have prefix) Function declared outside of extension

Explanation: If you include something within a function it is in the variable scope of that function. You can then explicitly add global variables to that scope as per your wishes. Then you can ensure that local variables are safely tucked away in a special global array. At the same time you can ensure that functions declared in extensions are prefixed. It may seem a bit messy but it works fine.

grep is your friend.


  • you can also make sure the extension vars are available for the callbacks

    grep is your friend.

  • and also enforce the same naming convention for constants too. Another idea is have a short prefix for each extension. That would make sure the names aren’t too long say 3 letters and one digit.

    grep is your friend.

Sign In or Register to comment.