On Wed, 17 Jul 2002 17:46:25 -0400, you wrote:

>Chris, thanks for describing your method. The reason I really dislike
>Functions in PHP is because you have to pass every variable needed by a
>function, even if that variable is global in the main script, which is a
>pain in the ass when a function needs a long string of variables. It makes
>it easier to forget a variable in the list and can make the code look messy.
>
>So, that's why I prefer includes, because the code is operating on the same
>level as the main script and can easily use variables set locally without
>making them global.
>
>I'll use a function if it only needs one or two variables passed to it, but,
>I find myself using more Includes than Functions because of the variable
>passing necessary. 

Maybe I'm misunderstanding what you guys are talking about here, but
it seems to me that you believe that including a block of code and
calling it as a function are just two different approaches to the same
thing, and nothing more.  It's true that they can be very similar, but
I think you are overlooking one of the major benefits of functions.

You speak of passing variables and making them global as if it were
simply a necessary evil.  Have you stopped to consider that there are
times that variable scope works in your favor?  There are times when
you do NOT want variable $a inside a function to be the same as
variable $a outside of it.

Functions (and classes) are all about code modularization.  Let us say
that you have a particular set of code that you know you are going to
need to execute several times.  So you decide to put it in an external
file and include() that code whenever you need it.  In this case, all
of the variables you use inside that code are global.  But, this is
not a problem because you are aware of which variables are in use, so
there are no conflicts.

But let us say that later on you, or some other programmer entirely,
want to use your code again in a completely different program.  Now,
because all of your variables are global, you (or they) have to stop
and think...."now lets see, this code references $var_a and
$var_b...am I using those variables already for other purposes?"  If
the new program is already using those variables, for some other
purpose altogether, then suddenly your included code has clobbered
those variables, and now you have bugs.  You have introduced a
namespace conflict, and this is an example of poorly seperated
functionality.

The whole idea behind modularization is that you are creating a "black
box" so to speak, a chunk of code that performs one function, and one
function only.  It should present a *minimal* interface to the outside
world.  This means that if I have a black box that say, gives me the
current local time in a particular timezone, I should be able to feed
it the name of the timezone, and it should give me back the current
time.  I should NOT have to know how it works inside, or care.  I feed
it one value, and it returns another value.  Now, if I have
implemented this code as a simple block that is include()'ed, now I
suddenly have to concern myself with how it works...i.e. I have to
know, if nothing else, what variable names it is expecting, and I have
to make sure that I am not clobbering any of those variables inside my
main script.

As far as I know, every non-trivial programming language (or scripting
language) has the concept of variable scope.  It behaves differently,
but the concept is still there.  For example, in Perl, a variable that
is referenced in a function (or subroutine) is global by default,
unless you use the "my" keyword, which makes it only visible inside
the subroutine.  PHP is backwards from this...it defaults to assuming
you want a local copy of the variable, and only makes it global if you
tell it to.  But imagine if there was no such concept as variable
scope, and all variables were global.  We'd end up with variables
names like $a3t463tew34 just to make sure we're not conflicting with
another named variable...

If you're saying that you have a group of variables, let us say 10 or
15 or so, that you want to be global EVERYWHERE, then there are easy
ways to accomplish that, and still retain the clean seperation of
functionality that a function or a class would give you.  I would put
the names of the variables inside an array and then globalize them by
iterating through the array.  Example:

$superglobals = array("var1", "var2", "var3", "var4", "var5", "...");

Now inside the function you can do this:

function somefunction ($somevar) {

  global $superglobals;
  foreach($superglobals as $varname) {
    global $$varname; //resolves to $var1, $var2, $var3, etc.
  }

  //Other stuff here

}

If I have completely misunderstood the thread here, please forgive my
rambling post. :-)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to