Declare variable as superglobal?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Super Mango

    Declare variable as superglobal?

    Hi -

    Is it possible to change the status of a variable to superglobal so
    it'll be valid inside functions without declaring it with "global"
    inside each function?

    Thanks -

  • Andy Hassall

    #2
    Re: Declare variable as superglobal?

    On 23 Aug 2005 12:01:04 -0700, "Super Mango" <liebermann@gma il.com> wrote:
    [color=blue]
    >Is it possible to change the status of a variable to superglobal so
    >it'll be valid inside functions without declaring it with "global"
    >inside each function?[/color]

    Not as far as I'm aware, I don't think this is exposed through a PHP API.

    I wonder if it's possible to write a loadable extension that could call the
    appropriate Zend API to superglobalify a variable, though.

    --
    Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
    http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

    Comment

    • Jacob Atzen

      #3
      Re: Declare variable as superglobal?

      On 2005-08-23, Super Mango <liebermann@gma il.com> wrote:[color=blue]
      > Is it possible to change the status of a variable to superglobal so
      > it'll be valid inside functions without declaring it with "global"
      > inside each function?[/color]

      You are aware of $_GLOBALS right?

      --
      Cheers,
      - Jacob Atzen

      Comment

      • JDS

        #4
        Re: Declare variable as superglobal?

        On Tue, 23 Aug 2005 12:01:04 -0700, Super Mango wrote:
        [color=blue]
        > Hi -
        >
        > Is it possible to change the status of a variable to superglobal so
        > it'll be valid inside functions without declaring it with "global"
        > inside each function?
        >
        > Thanks -[/color]

        All you have to do is put it inside one of the superglobal arrays. Use
        those arrays directly inside the function.

        e.g. instead of

        function donut(){
        global $holes;
        return $holes - 1;
        }

        do

        function donut(){
        return $GLOBALS['holes'] - 1;
        /* or $_SESSION['holes'] or $_POST['holes'] or other superglobal */
        }

        However, the real answer to your question is DO NOT USE GLOBALS INSIDE
        FUNCTIONS!!!! You are just asking for trouble down the line, take my word
        for it.

        --
        JDS | jeffrey@example .invalid
        | http://www.newtnotes.com
        DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

        Comment

        • Chung Leong

          #5
          Re: Declare variable as superglobal?

          I don't think that can be implemented. The list of autoglobals isn't
          stored in a pre-request structure. If you append a new variable to it,
          it's going to remain there after a script finishes executing. You can
          certainly write an extension that uses its own autoglobals however.

          Comment

          • Super Mango

            #6
            Re: Declare variable as superglobal?

            Thanks,

            My idea is to set variables like
            $b="<BR>"
            so it's kind of shortcuts.
            If I use the super globals, I'd rather write "<BR>" directly.

            Sorry for not explaining that in the first place.

            Comment

            • Jerry Stuckle

              #7
              Re: Declare variable as superglobal?

              Super Mango wrote:[color=blue]
              > Thanks,
              >
              > My idea is to set variables like
              > $b="<BR>"
              > so it's kind of shortcuts.
              > If I use the super globals, I'd rather write "<BR>" directly.
              >
              > Sorry for not explaining that in the first place.
              >[/color]

              define('br', '<br>');

              echo br;

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Super Mango

                #8
                Re: Declare variable as superglobal?

                Great! The "define" did it.

                Any Idea for the cases that I want to use a variable after all (for
                counter or for mysql connection)?

                Thanks again.

                Comment

                • Oliver Grätz

                  #9
                  Re: Declare variable as superglobal?

                  Super Mango schrieb:[color=blue]
                  > Great! The "define" did it.
                  >
                  > Any Idea for the cases that I want to use a variable after all (for
                  > counter or for mysql connection)?[/color]

                  Good you elaborate on that?

                  For example, I hide the connecting stuff from everyday code. Looks
                  something like this:

                  class MySQL
                  {
                  private static $connection=nul l;
                  protected static function connect()
                  {
                  if (self::$connect ion != null) return;
                  self::$connecti on=mysql_connec t('bla','blah', 'bla');
                  // read from config...
                  }
                  public function query($sql)
                  {
                  self::connect() ;
                  return mysql_query(sel f::$connection, $sql);
                  }
                  }

                  In my everyday code I just have to

                  $result=MySQL:: query('SELECT * FROM test');

                  No explicit connecting required. Of course you can go for functions but
                  I'm all for the OOP way (even if it's just static stuff like here).

                  AllOLLi

                  Comment

                  • R. Rajesh Jeba Anbiah

                    #10
                    Re: Declare variable as superglobal?

                    Super Mango wrote:[color=blue]
                    > Hi -
                    >
                    > Is it possible to change the status of a variable to superglobal so
                    > it'll be valid inside functions without declaring it with "global"
                    > inside each function?[/color]

                    runkit <http://in.php.net/runkit>

                    --
                    <?php echo 'Just another PHP saint'; ?>
                    Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

                    Comment

                    Working...