Permanently "global" ?

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

    #1

    Permanently "global" ?

    I have an array/hash that stores path information for my app. As in,
    what directory this is in, what directory that's in, what the name of
    the site is, what the products are called, etc. It's called $glb.

    So, every function so far looks like this:

    function something() {
    global $glb;
    }

    over and over and over

    How do I make $glb just always be global? I'm assuming there's a way
    to do this, otherwise let me know and I'll parachute out of this
    language and run back home to perl.

    thanks
    mrb




    ------------------------------------------
    Signature:
    Never buy the services of newsfeed.com. I am a paying customer but
    I'm using google to post messages, so that I can avoid their damn
    advertisement showing up in every post I make.
    ------------------------------------------
  • Pedro Graca

    #2
    Re: Permanently "global&qu ot; ?

    mrbog wrote:[color=blue]
    > So, every function so far looks like this:
    >
    > function something() {
    > global $glb;
    > }
    >
    > over and over and over
    >
    > How do I make $glb just always be global? I'm assuming there's a way[/color]
    (snip)


    You have the permanently "global" array $GLOBALS, so you may do

    <?php
    function something() {
    $glb = $GLOBALS['glb'];
    // do whatever you want with $glb, but don't forget it is a local
    // variable. If you want to change it in global scope use the full
    // variable.

    $glb['dirname'] = 'changed inside something()'; // does not work!
    $GLOBALS['glb']['appname'] = 'changed inside something()'; // this works
    }

    function otherthing() {
    $glb = $GLOBALS['glb'];
    echo 'dirname = ', $glb['dirname'], "\n";

    // or, if you don't want de declare a local variable
    echo 'appname = ', $GLOBALS['glb']['appname'], "\n";
    }

    $glb['dirname'] = 'globally set';
    $glb['appname'] = 'globally set';
    otherthing();
    something();
    otherthing();
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Eric Bohlman

      #3
      Re: Permanently &quot;global&qu ot; ?

      dterrors@hotmai l.com (mrbog) wrote in
      news:cbd4bb52.0 402141810.359d2 4c5@posting.goo gle.com:
      [color=blue]
      > I have an array/hash that stores path information for my app. As in,
      > what directory this is in, what directory that's in, what the name of
      > the site is, what the products are called, etc. It's called $glb.
      >
      > So, every function so far looks like this:
      >
      > function something() {
      > global $glb;
      > }
      >
      > over and over and over
      >
      > How do I make $glb just always be global? I'm assuming there's a way
      > to do this, otherwise let me know and I'll parachute out of this
      > language and run back home to perl.[/color]

      Hmm. If you had crossposted this to comp.lang.perl. misc, most of the
      regulars there would have told you that regardless of what language you're
      using, you need to rethink your programming style if your functions/subs
      have to access more than a handful of globals. It's simply a fact of life
      (often learned through bitter experience) that when you have lots of
      functions that communicate with each other through global variables, things
      very rapidly get messy and unmaintainable because your code depends on
      "action at a distance."

      Six months after you write a program, you need to change the code in a
      function, look at it and see a bunch of "global..." and now you have to
      look at every other function that uses those variables to make sure their
      behavior won't be adversely affected by any changes you make (it's even
      worse in Perl, since global variables are accessible inside subs unless you
      explicitly override them, so Perl regulars who have got bitten by this
      sort of thing will yell at you if you don't give your variables the
      narrowest scope possible). And if you ever have to ask yourself "can I
      give this name to my new variable without stepping on some other code"
      you're suffering from Creeping Globalism.

      OOP (specifically the encapsulation aspects) is one way of getting around
      the "action at a distance" problem but it's not, contrary to what some
      dogmatists might say, the only way. Every common programming language, for
      example, allows you to pass parameters by reference, and you can make good
      use of that by passing references to shared parameters.

      So basically, what you're asking for is a facility to give yourself enough
      rope to hang yourself. Perl, to some extent, does that, but a lot of Perl
      programmers misunderstand the reason for it. Larry Wall was aware of the
      fact that there are many forms of programming discipline and that, despite
      all the religious wars, what really matters is that you pick a particular
      form of programming discipline and stick to it. The naive Perl programmer
      regards Perl as an "undiscipli ned" language. The experienced Perl
      programmer regards Perl as a "bring your own discipline (BYOD)" language.
      The lesson here is applicable to other languages, including PHP. If your
      functions are heavily dependent on global variables, you need to refactor
      your code.

      Comment

      • mrbog

        #4
        Re: Permanently &quot;global&qu ot; ?

        Pedro Graca <hexkid@hotpop. com> wrote in message news:<c0mlf0$19 baq7$1@ID-203069.news.uni-berlin.de>...[color=blue]
        > mrbog wrote:[color=green]
        > > So, every function so far looks like this:
        > >
        > > function something() {
        > > global $glb;
        > > }
        > >
        > > over and over and over
        > >
        > > How do I make $glb just always be global? I'm assuming there's a way[/color]
        > (snip)
        >
        >
        > You have the permanently "global" array $GLOBALS, so you may do
        >
        > <?php
        > function something() {
        > $glb = $GLOBALS['glb'];
        > // do whatever you want with $glb, but don't forget it is a local
        > // variable. If you want to change it in global scope use the full
        > // variable.
        >
        > $glb['dirname'] = 'changed inside something()'; // does not work!
        > $GLOBALS['glb']['appname'] = 'changed inside something()'; // this works
        > }
        >
        > function otherthing() {
        > $glb = $GLOBALS['glb'];
        > echo 'dirname = ', $glb['dirname'], "\n";
        >
        > // or, if you don't want de declare a local variable
        > echo 'appname = ', $GLOBALS['glb']['appname'], "\n";
        > }
        >
        > $glb['dirname'] = 'globally set';
        > $glb['appname'] = 'globally set';
        > otherthing();
        > something();
        > otherthing();
        > ?>[/color]

        I don't understand- there's no gain there, right? I still have to
        include that extra line at the top of every one of my functions, isn't
        that just as bad?

        What I want to do (in a short example) is:

        function something() {
        print $glb[someglobal];
        }

        and have it print the global value. I don't want to have to do this
        over and over:

        function something() {
        global $glb;
        print $glb[someglobal];
        }

        How do I say in my code "Just always make $glb in the global scope,
        everywhere, don't make me keep telling you."

        Comment

        • André Næss

          #5
          Re: Permanently &quot;global&qu ot; ?

          Eric Bohlman:
          [color=blue]
          > dterrors@hotmai l.com (mrbog) wrote in
          > news:cbd4bb52.0 402141810.359d2 4c5@posting.goo gle.com:
          >[color=green]
          >> I have an array/hash that stores path information for my app. As in,
          >> what directory this is in, what directory that's in, what the name of
          >> the site is, what the products are called, etc. It's called $glb.
          >>
          >> So, every function so far looks like this:
          >>
          >> function something() {
          >> global $glb;
          >> }
          >>
          >> over and over and over
          >>
          >> How do I make $glb just always be global? I'm assuming there's a way
          >> to do this, otherwise let me know and I'll parachute out of this
          >> language and run back home to perl.[/color]
          >
          > Hmm. If you had crossposted this to comp.lang.perl. misc, most of the
          > regulars there would have told you that regardless of what language you're
          > using, you need to rethink your programming style if your functions/subs
          > have to access more than a handful of globals. It's simply a fact of life
          > (often learned through bitter experience) that when you have lots of
          > functions that communicate with each other through global variables,
          > things very rapidly get messy and unmaintainable because your code depends
          > on "action at a distance."
          >
          > OOP (specifically the encapsulation aspects) is one way of getting around
          > the "action at a distance" problem but it's not, contrary to what some
          > dogmatists might say, the only way. Every common programming language,
          > for example, allows you to pass parameters by reference, and you can make
          > good use of that by passing references to shared parameters.[/color]

          When you pass by reference you can alter the value of an object/entity that
          might possibly be used in many other places. This does not in any way
          remove you from the problem, on the contrary, it creates it. The only way
          to avoid it would be to pass by *value*. This is really about programming
          with side-effects and OOP is all about side-effects. Functionaly languages
          try to remove the need for side-effects because they are bad. But in some
          cases the side-effects are a necessary evil.
          [color=blue]
          > So basically, what you're asking for is a facility to give yourself enough
          > rope to hang yourself.[/color]

          All he wanted was a simple way to share configuration data, data which is
          usually common to the entire application and never changes. Now this has
          the characteristics of constants, and IMO that's what he should use. I
          always use constants for configuration like this because one usually only
          need a handful. Constants have the same scope as superglobals and can be
          directly accessed from within functions.

          A second solution would be to store the data in one of the superglobals, but
          in don't think that's a very good solution because the superglobals have
          very specific uses, and you might confuse yourself easily if you start
          putting all sorts of strange data in there.

          André Næss


          Comment

          • Matthias Esken

            #6
            Re: Permanently &quot;global&qu ot; ?

            dterrors@hotmai l.com (mrbog) schrieb:
            [color=blue]
            > I don't understand- there's no gain there, right? I still have to
            > include that extra line at the top of every one of my functions, isn't
            > that just as bad?
            >
            > What I want to do (in a short example) is:
            >
            > function something() {
            > print $glb[someglobal];
            > }
            >
            > and have it print the global value. I don't want to have to do this
            > over and over:
            >
            > function something() {
            > global $glb;
            > print $glb[someglobal];
            > }
            >
            > How do I say in my code "Just always make $glb in the global scope,
            > everywhere, don't make me keep telling you."[/color]

            There is no way to make $glb global. Of course you could use $_GLOBAL.
            See http://www.php.net/manual/en/languag...predefined.php for
            details.

            Example:

            $_GLOBALS['var1'] = 'foo';
            $_GLOBALS['var2'] = array (
            'foo' => 23,
            'bar' => 41
            );

            function something() {
            echo ($_GLOBALS['var1']);
            }

            function something2() {
            echo ($_GLOBALS['var1']['foo']);
            $_GLOBALS['var2']['bar'] += 1;
            }

            something();
            something2();
            echo ($_GLOBALS['var1']['bar']);


            Regards,
            Matthias

            Comment

            • Matthias Esken

              #7
              Re: Permanently &quot;global&qu ot; ?

              Matthias Esken <muelleimer2004 nospam@usenetve rwaltung.org> schrieb:
              [color=blue]
              > There is no way to make $glb global. Of course you could use $_GLOBAL.[/color]

              I knew I'd make an error. :-)

              The correct name of the array is $GLOBALS.

              Regards,
              Matthias

              Comment

              • mrbog

                #8
                Re: Permanently &quot;global&qu ot; ?

                Matthias Esken <muelleimer2004 nospam@usenetve rwaltung.org> wrote in message news:<c0o21p.u0 .1@usenet.esken .de>...[color=blue]
                > Matthias Esken <muelleimer2004 nospam@usenetve rwaltung.org> schrieb:
                >[color=green]
                > > There is no way to make $glb global. Of course you could use $_GLOBAL.[/color]
                >
                > I knew I'd make an error. :-)
                >
                > The correct name of the array is $GLOBALS.
                >
                > Regards,
                > Matthias[/color]

                Matthias, thank you. This is exactly what I needed.
                thanks much,

                mrb

                Comment

                • mrbog

                  #9
                  Re: Permanently &quot;global&qu ot; ?

                  > Hmm. If you had crossposted this to comp.lang.perl. misc, most of the[color=blue]
                  > regulars there would have told you that regardless of what language you're
                  > using, you need to rethink your programming style if your functions/subs
                  > have to access more than a handful of globals.[/color]

                  Hello?? All I want is "a handful of globals". If I cross-posted to
                  comp.lang.perl. misc, they'd agree vehemently. This is one of the many
                  absurdities of PHP.

                  Look, I've been writing applications professionally for ten years.
                  I've been writing perl since just after perl 4 was released. Don't
                  tell me I don't know how to write proper perl. If one of us needs a
                  computer science lesson, it's you.

                  PHP is the only language (well except for obscure languages that no
                  one uses) that requires the programmer to explicitly bring a global
                  into scope. The whole point of a global is that it's always in scope.
                  That's what a global is supposed to be.

                  And btw, perl doesn't actually have global variables. Every perl
                  variable is in a package. The default package is main. Go try it in
                  your (perl) code:

                  $v="wef";
                  print $main::v;

                  This could (SHOULD) be the same in PHP but PHP doesn't have
                  namespaces, which is why it continues to be a "toy" language, which
                  you can't even architect with UML.

                  You're telling me that unless a language forces me to explicitly bring
                  globals into scope, that it's "giving me enough rope to hang
                  myself"?!? Hmm so I guess that's true of every language except PHP,
                  right? Brilliant, so you've taken a design flaw in PHP and made it
                  into it's greatest virtue. "A flat tire stops you from speeding-
                  flat tires are a feature, not a flaw."

                  (Matthias solved it for me, thanks to Matt)

                  mrb

                  Comment

                  • Chung Leong

                    #10
                    Re: Permanently &quot;global&qu ot; ?

                    What you have are constants. In general, it's best to avoid using variables
                    as constants (because variables are not constant). Constants always have
                    global scope.

                    You can't create your own super globals. You can hijack one of the existing
                    ones though:

                    $_ENV = $glb;

                    Uzytkownik "mrbog" <dterrors@hotma il.com> napisal w wiadomosci
                    news:cbd4bb52.0 402141810.359d2 4c5@posting.goo gle.com...[color=blue]
                    > I have an array/hash that stores path information for my app. As in,
                    > what directory this is in, what directory that's in, what the name of
                    > the site is, what the products are called, etc. It's called $glb.
                    >
                    > So, every function so far looks like this:
                    >
                    > function something() {
                    > global $glb;
                    > }
                    >
                    > over and over and over
                    >
                    > How do I make $glb just always be global? I'm assuming there's a way
                    > to do this, otherwise let me know and I'll parachute out of this
                    > language and run back home to perl.
                    >
                    > thanks
                    > mrb
                    >
                    >
                    >
                    >
                    > ------------------------------------------
                    > Signature:
                    > Never buy the services of newsfeed.com. I am a paying customer but
                    > I'm using google to post messages, so that I can avoid their damn
                    > advertisement showing up in every post I make.
                    > ------------------------------------------[/color]


                    Comment

                    • Cecile Muller

                      #11
                      Re: Permanently &quot;global&qu ot; ?

                      > PHP is the only language (well except for obscure languages that no[color=blue]
                      > one uses) that requires the programmer to explicitly bring a global
                      > into scope. The whole point of a global is that it's always in scope.
                      > That's what a global is supposed to be.[/color]

                      And that's why $GLOBALS is there. When you work with several people,
                      it prevents from messing with the other people's variables given they
                      are local unless speficied otherwise. Also when you will re-read your
                      code in a year, you will know immediatly if your variable is local or
                      global. Btw, you could also send it as parameter of the function
                      instead, so that you don't need to change every occurence in all the
                      functions using it if you change the name of the global variable, plus
                      it will be easier for re-using the function in other scripts.

                      Comment

                      • Chung Leong

                        #12
                        Re: Permanently &quot;global&qu ot; ?

                        Uzytkownik "mrbog" <dterrors@hotma il.com> napisal w wiadomosci
                        news:cbd4bb52.0 402151113.3eed0 e23@posting.goo gle.com...[color=blue]
                        > PHP is the only language (well except for obscure languages that no
                        > one uses) that requires the programmer to explicitly bring a global
                        > into scope. The whole point of a global is that it's always in scope.
                        > That's what a global is supposed to be.[/color]

                        Well, the alternative would be having to declare variables as local, like
                        Javascript. In C/C++ it makes sense, since you need to declare the type
                        anyway. But for a typeless language, PHP's arrangement is quite reasonable.
                        It's hilarious sometimes when I see JS code where properties of the window
                        object are used as looping variables.


                        Comment

                        Working...