Re: [PHP] Re: Includes vs. Functions

2002-07-18 Thread Michael Sims

On Thu, 18 Jul 2002 08:40:08 -0600, you wrote:

See below...

>At 09:20 PM 7/17/02 -0500, Michael Sims wrote:
>
>>$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
>>
>>}
>
>You're working too hard!
>
>function somefunction( $somevar ) {
>
>global $suberglobals;
>
>extract( $superglobals );
>
>//Other stuff here
>
>}

Sorry, but your version is not functionally equivalent to mine.  The
call to extract() is creating locally scoped versions of those
variables which do NOT reference the global versions.  For proof, go
here:

http://mhs-dev.crye-leike.com/test.php

Thanks for the pointer to extract() though, I had never used it
before.

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




Re: [PHP] Re: Includes vs. Functions

2002-07-18 Thread Rick Widmer

At 09:20 PM 7/17/02 -0500, Michael Sims wrote:


>$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
>
>}


You're working too hard!


function somefunction( $somevar ) {

global $suberglobals;

extract( $superglobals );

//Other stuff here

}

Rick



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




RE: [PHP] Re: Includes vs. Functions

2002-07-18 Thread Jay Blanchard

[snip]
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.
[/snip]

Includes vs. Functions? How about includes that have functions, which a lot
of developers do. For instance, I have a function set up for database
connections. I use it by including it with pages that require database
connections. I think this probably should be Functions vs. Blocks of Code
(that might be placed in an included file). Actually I see this as being
potentially messier, IMHO. I will have to remember many variable names that
are used in the included file, rather than passing variables I am using
locally to functions with clearly thought out names. Example;

a. I need a variable to hold this standard deviation result. Since I do the
stdv calculation in the block I included I need to go see what the name of
the variable is and use that, so I don't muck up the code.

b. calcStdDev($myCurrentVariable); oh, I need one for here too so,
calcStdDev($anotherVariable);

I just think that you are selling yourself short, and ultimately your code
will be harder to document and maintain. Using blocks is OK, if that block
can be 'functionalized' I say do it.

For efficiency in processing it really will not make much difference in an
included file whether that file is blocks, or functions.

Jay

"You’re just jealous because the voices are talking to me"

*
* Want to meet other PHP developers *
* in your area? Check out:  *
* http://php.meetup.com/*
* No developer is an island ... *
*



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




RE: [PHP] Re: Includes vs. Functions

2002-07-17 Thread Martin Towell

> >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.

[snip]

> 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.

[snip]

To add to this. If you're passing too many values to a function, then maybe
that function isn't broken down enough. I remember hearing/reading that if
you need to pass more than, I think, 5 or 6 values to a function, then
you're passing too many.

Martin

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




Re: [PHP] Re: Includes vs. Functions

2002-07-17 Thread Michael Sims

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




[PHP] Re: Includes vs. Functions

2002-07-17 Thread Monty

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. 

Tom



> From: [EMAIL PROTECTED] (Chris Crane)
> Organization: Inxdesign.com
> Reply-To: "Chris Crane" <[EMAIL PROTECTED]>
> Newsgroups: php.general
> Date: Wed, 17 Jul 2002 17:05:59 -0400
> To: [EMAIL PROTECTED]
> Subject: Re: Includes vs. Functions
> 
> I am not sure about efficiency, myself and I often ask this without much if
> anything at all for a response. However, I am the opposite of you, in that I
> prefer to create a library file containing all the functions. To make
> developing the page in something like Dreamweaver or Frontpage easier, I
> global all the variables of a given function, like say Function
> StockQuote($sym) { }. They I include the library file in my PHP
> webpage.Something like this...
> 
> 
> 
> ~blah
> 
> 
> 
> 
> various html
> ~blah
> ~blah
> 
> 
> ~blah
> ~blah
> 
> 
> 
> In the library file, I global the variables;
> 
> function StockQuote($sym) {
> 
> global $Stock_LastPrice, $Stock_Position;
> get the data...
> do something with the data...
> $Stock_LastPrice = This minus that blah blah;
> }
> 
> I can use this variable anywhere I would like and it is very WYSIWYG
> friendly.
> 
> I am not sure how efficient it is though as I mentioned above.
> 


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




[PHP] Re: Includes vs. Functions

2002-07-17 Thread Chris Crane

I am not sure about efficiency, myself and I often ask this without much if
anything at all for a response. However, I am the opposite of you, in that I
prefer to create a library file containing all the functions. To make
developing the page in something like Dreamweaver or Frontpage easier, I
global all the variables of a given function, like say Function
StockQuote($sym) { }. They I include the library file in my PHP
webpage.Something like this...



~blah




various html
~blah
~blah


~blah
~blah



In the library file, I global the variables;

function StockQuote($sym) {

global $Stock_LastPrice, $Stock_Position;
get the data...
do something with the data...
$Stock_LastPrice = This minus that blah blah;
}

I can use this variable anywhere I would like and it is very WYSIWYG
friendly.

I am not sure how efficient it is though as I mentioned above.



"Monty" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> I generally prefer to include various blocks of code in templates using
> include_once() rather than load a functions library and make calls to
those
> functions within the script.
>
> Is there a big difference in efficiency and speed of includes vs. custom
> functions? I like includes because it's easier to drop blocks of code in a
> page design without disrupting the design of the page very much.
>
> Thanks,
>
> Monty
>



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