List predefined variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?=

    List predefined variables

    Is there any way to tell PHP predefined variables ($GLOBALS, $argv,
    $argc, $_GET, $_POST…) from *global* user-defined variables? Neither
    $GLOBALS nor get_defined_var s() put user data apart.

    I’m writing a class to generate PHP definition files for syntax
    highlighting with two features:

    1. It takes data from current install (e.g., you get function names from
    loaded extensions).
    2. You can choose whether to include user data or not.

    It’s been very easy with functions and constants but I can’t figure out
    how to deal with variables, rather than hard-coding the names of
    predefined vars... I’ve kind of automated this hard-coding but it still
    smells of lame workaround.



    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor al baño María: http://www.demogracia.com
    --
  • C. (http://symcbean.blogspot.com/)

    #2
    Re: List predefined variables

    On 2 Oct, 12:24, "Álvaro G. Vicario"
    <alvaroNOSPAMTH A...@demogracia .comwrote:
    Is there any way to tell PHP predefined variables ($GLOBALS, $argv,
    $argc, $_GET, $_POST…) from *global* user-defined variables? Neither
    $GLOBALS nor get_defined_var s() put user data apart.
    >
    I’m writing a class to generate PHP definition files for syntax
    highlighting with two features:
    >
    1. It takes data from current install (e.g., you get function names from
    loaded extensions).
    2. You can choose whether to include user data or not.
    >
    It’s been very easy with functions and constants but I can’t figure out
    how to deal with variables, rather than hard-coding the names of
    predefined vars... I’ve kind of automated this hard-coding but it still
    smells of lame workaround.
    >
    Why not just iterate over $_GLOBALS, and for each entry, check if it
    is in scope:

    function get_non_supergl obal_globals()
    {
    $result=array() ;
    foreach ($_GLOBALS as $anon_name =$anon_value) {
    if (($anon_name!=' anon_name') && ($anon_name!='a non_value')) {
    if ($_GLOBALS[$anon_name]===$$anon_name) {
    // its a superglobal
    } else {
    // its a user global
    $result[]=$anon_name;
    }
    }
    }
    }

    - this is bit of a hack too - I think there are circumstances where it
    won't work. I'd go with a predefined list of superglobals.

    C.

    Comment

    • =?windows-1252?Q?=22=C1lvaro_G=2E_Vicario=22?=

      #3
      Re: List predefined variables

      C. (http://symcbean.blogspot.com/) escribió:
      >Is there any way to tell PHP predefined variables ($GLOBALS, $argv,
      >$argc, $_GET, $_POST…) from *global* user-defined variables? Neither
      >$GLOBALS nor get_defined_var s() put user data apart.
      function get_non_supergl obal_globals()
      {
      $result=array() ;
      foreach ($_GLOBALS as $anon_name =$anon_value) {
      if (($anon_name!=' anon_name') && ($anon_name!='a non_value')) {
      if ($_GLOBALS[$anon_name]===$$anon_name) {
      // its a superglobal
      } else {
      // its a user global
      $result[]=$anon_name;
      }
      }
      }
      }
      I had tried this approach but it didn't work because superglobals cannot
      be used with variable variables. However, I've found a workaround
      with... ahem... eval():

      <?php
      foreach ($GLOBALS as $anon_name =$anon_value) {
      if (($anon_name!=' anon_name') && ($anon_name!='a non_value')) {
      $is_superglobal = eval('return isset($' . $anon_name . ');');
      echo $anon_name . ': ' . ($is_supergloba l ? 'superglobal' : 'user') .
      "\n";
      }
      }
      ?>

      GLOBALS: superglobal
      argv: user
      argc: user
      _POST: superglobal
      _GET: superglobal
      _COOKIE: superglobal
      _FILES: superglobal
      _SERVER: superglobal
      my_variable: user
      foo: user

      Of course, I guess there's nothing I can do with predefined variables
      that are not superglobals (e.g. argv & argc).
      - this is bit of a hack too - I think there are circumstances where it
      won't work. I'd go with a predefined list of superglobals.
      Well, I need some predefined lists anyway ("if", "else", "new"...) so I
      guess it won't be a great deal. It's easier than writing a Zend
      extension ;-)

      Thank you for your suggestion.


      --
      -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
      -- Mi sitio sobre programación web: http://bits.demogracia.com
      -- Mi web de humor al baño María: http://www.demogracia.com
      --

      Comment

      Working...