Whats PHPs version of ASPs application scope variable

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

    Whats PHPs version of ASPs application scope variable

    In ASP you can create a variable that is accessible by all scripts in
    an application.

    Is this possible in PHP? Storing a multi-dimensional array in memory
    has much greater performance benefits than storing in a database or
    session (which just gets saved to disk anyway). Id like to have my
    commonly used application variables in memory.

    Is this possible or is it one of the few flaws of PHP?

    -Nick
  • Tom Thackrey

    #2
    Re: Whats PHPs version of ASPs application scope variable


    On 29-Dec-2003, nboutelier@hotm ail.com (Nick) wrote:
    [color=blue]
    > In ASP you can create a variable that is accessible by all scripts in
    > an application.
    >
    > Is this possible in PHP? Storing a multi-dimensional array in memory
    > has much greater performance benefits than storing in a database or
    > session (which just gets saved to disk anyway). Id like to have my
    > commonly used application variables in memory.
    >
    > Is this possible or is it one of the few flaws of PHP?[/color]



    scroll down to the global keyword


    --
    Tom Thackrey

    tom (at) creative (dash) light (dot) com
    do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

    Comment

    • Justin Koivisto

      #3
      Re: Whats PHPs version of ASPs application scope variable

      Nick wrote:[color=blue]
      > Is this possible in PHP? Storing a multi-dimensional array in memory
      > has much greater performance benefits than storing in a database or
      > session (which just gets saved to disk anyway). Id like to have my
      > commonly used application variables in memory.
      >
      > Is this possible or is it one of the few flaws of PHP?[/color]

      There are a few ways to do this. One way is to use the 'global' keyword
      in your functions. Another is to use the $GLOBALS array, but the one I
      like most is to create a custom ini file and use parse_ini_file to read
      the data. This makes for the easiest editing - especially by users.

      Then if you set the auto_prepend_fi le setting via a .htaccess file, this
      would be similar to using a globals.asp file (I think that's what it was
      called anyway).

      /includes/globals.php.ini
      --------------------------
      <?php exit; // in case it's called by a browser ?>
      [section1]
      var1 = val1
      var2 = val2

      [section2]
      var1 = val2.1
      var2 = val2.2

      /includes/globals.php
      ---------------------
      <?php
      $CFG=parse_ini_ file(dirname(__ FILE__).'/globals.ini.php ',TRUE);
      ?>

      /test.php
      ---------
      <?php
      echo'<pre>';
      print_r($CFG);
      echo'</pre>';
      ?>

      /.htaccess
      ----------
      php_value auto_prepend_fi le "/home/user/www/includes/globals.php"

      The above would then output this:
      Array
      (
      [section1] => Array
      (
      [var1] => val1
      [var2] => val2
      )

      [section2] => Array
      (
      [var1] => val2.1
      [var2] => val2.2
      )

      )

      If you don't set the second argument to parse_ini_file, then you'd get:
      Array
      (
      [var1] => val2.1
      [var2] => val2.2
      )


      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.

      Comment

      • Daniel Tryba

        #4
        Re: Whats PHPs version of ASPs application scope variable

        Nick <nboutelier@hot mail.com> wrote:[color=blue]
        > In ASP you can create a variable that is accessible by all scripts in
        > an application.
        >
        > Is this possible in PHP? Storing a multi-dimensional array in memory
        > has much greater performance benefits than storing in a database or
        > session (which just gets saved to disk anyway).[/color]

        You might use shared memory and semphores for this

        [color=blue]
        > Id like to have my
        > commonly used application variables in memory.[/color]

        If it's static data a simple include()/require() is better suited (might
        be in the diskcache).

        --

        Daniel Tryba

        Comment

        • Agelmar

          #5
          Re: Whats PHPs version of ASPs application scope variable

          PHP has no concept of an "applicatio n", unlike ASP.Net. PHP is a scripting
          language, and from the view of PHP, the "applicatio n" is the current file
          plus anything include()d or require()d.

          Your best shot is probably either just writing out everything to a file like
          vars.inc.php which you include on all pages (and re-writing that file as
          necessary), or storing the information in some sort of database if that
          turns out to be more practical.

          Some people may call this a shortfall, others just see it as a design
          philosophy. ASP.Net is designed around applications and tasks, PHP is
          designed around scripts and procedures. (And no, I don't mean to start some
          tech flame war, or anything like that. I'm simply saying that ASP.Net has a
          concept of applications in the sense of multiple source files compiled into
          a single application, supports OOP fully etc, whereas PHP is extremely well
          suited towards procedural programming and scripting, but lacks the
          facilities for full OOP and the concept of an "applicatio n" as opposed to
          simply a collection of code.)

          "Nick" <nboutelier@hot mail.com> wrote in message
          news:ce0d844c.0 312291142.51136 35b@posting.goo gle.com...[color=blue]
          > In ASP you can create a variable that is accessible by all scripts in
          > an application.
          >
          > Is this possible in PHP? Storing a multi-dimensional array in memory
          > has much greater performance benefits than storing in a database or
          > session (which just gets saved to disk anyway). Id like to have my
          > commonly used application variables in memory.
          >
          > Is this possible or is it one of the few flaws of PHP?
          >
          > -Nick[/color]


          Comment

          • FLEB

            #6
            Re: Whats PHPs version of ASPs application scope variable

            Regarding this well-known quote, often attributed to Nick's famous "29 Dec
            2003 11:42:50 -0800" speech:
            [color=blue]
            > In ASP you can create a variable that is accessible by all scripts in
            > an application.
            >
            > Is this possible in PHP? Storing a multi-dimensional array in memory
            > has much greater performance benefits than storing in a database or
            > session (which just gets saved to disk anyway). Id like to have my
            > commonly used application variables in memory.
            >
            > Is this possible or is it one of the few flaws of PHP?
            >
            > -Nick[/color]

            I'm not familiar with the ASP definition of an "Applicatio n". Does it
            consist of only one HTML output (as in "a lot of includes, but only one
            final output") or does the "Applicatio n" refer to multiple pages which
            perform a common function?

            If it's the former, AFAIK, any global variables, if you call them with
            "global" in any functions, persist into and out of included files. If
            you're talking the latter, then sessions are about the best way to go.



            --
            -- Rudy Fleminger
            -- sp@mmers.and.ev il.ones.will.bo w-down-to.us
            (put "Hey!" in the Subject line for priority processing!)
            -- http://www.pixelsaredead.com

            Comment

            • Savut

              #7
              Re: Whats PHPs version of ASPs application scope variable

              Damn guys you dont know anything about ASP, only Agelmar know what is the Application concept in ASP, in PHP, the only comparaison
              whould be Apache predefined variable, but you can't define your own as ASP do. The Application variable is all variables you
              created for the whole server, this mean every ASP websites on this server can use these variables. There is not equivalence in PHP.
              The only workaround whould be to use an include on all pages.
              How sad :(

              Savut

              "Nick" <nboutelier@hot mail.com> wrote in message news:ce0d844c.0 312291142.51136 35b@posting.goo gle.com...[color=blue]
              > In ASP you can create a variable that is accessible by all scripts in
              > an application.
              >
              > Is this possible in PHP? Storing a multi-dimensional array in memory
              > has much greater performance benefits than storing in a database or
              > session (which just gets saved to disk anyway). Id like to have my
              > commonly used application variables in memory.
              >
              > Is this possible or is it one of the few flaws of PHP?
              >
              > -Nick[/color]


              Comment

              • Justin Koivisto

                #8
                Re: Whats PHPs version of ASPs application scope variable

                Savut wrote:
                [color=blue]
                > Damn guys you dont know anything about ASP[/color]

                hmm... never did claim to know anything about it.
                [color=blue]
                > only Agelmar know what is the Application concept in ASP[/color]

                Then perhaps the OP should have described it a bit more in detail to
                give us a clue.
                [color=blue]
                > in PHP, the only comparaison
                > whould be Apache predefined variable, but you can't define your own as ASP do.[/color]

                Actually, you can - in httpd.conf using similar to:
                SetEnv TestString "Test String With Spaces"
                SetEnv TestNumber 3157851357

                This can go in the global for all hosted sites on the server to access,
                or inside VirtualHost directives on a per-domain basis. Also, if you
                want a number of domains to use them (but not all), you can place them
                in a separate file and use the Include directive inside VirtualHosts to
                get the values. You can also use that syntax in .htaccess files as well
                to define your variables on a per directory basis.

                You then access variables set with this method via the $_SERVER array.
                [color=blue]
                > The Application variable is all variables you
                > created for the whole server, this mean every ASP websites on this
                > server can use these variables. There is not equivalence in PHP.
                > The only workaround whould be to use an include on all pages.
                > How sad :([/color]

                Right, isn't that what I illustrated using parse_ini_file and
                auto_prepend in my post? ;) Only difference is you don't have to add a
                line of code to every file to include it.

                --
                Justin Koivisto - spam@koivi.com
                PHP POSTERS: Please use comp.lang.php for PHP related questions,
                alt.php* groups are not recommended.

                Comment

                Working...