472,096 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

Subroutines

Is this something that has ever been suggested, discussed, debated and/or
considered? By subroutine I mean a chunk of code that can be called like a
function but which executes in the global space. Yes, I know an include
file executes in the global space (if called from there), but when you have
a number of such chunks of code, it would be a lot cleaner if they could be
combined in one file.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #1
13 7042
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):
Is this something that has ever been suggested, discussed, debated and/or
considered? By subroutine I mean a chunk of code that can be called like a
function but which executes in the global space. Yes, I know an include
file executes in the global space (if called from there), but when you have
a number of such chunks of code, it would be a lot cleaner if they could be
combined in one file.


Do you mean functions?

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Manda tus dudas al grupo, no a mi buzón
-+ Send your questions to the group, not to my mailbox
--
Jul 17 '05 #2
Alan Little wrote:
Is this something that has ever been suggested, discussed, debated and/or
considered? By subroutine I mean a chunk of code that can be called like a
function but which executes in the global space. Yes, I know an include
file executes in the global space (if called from there), but when you have
a number of such chunks of code, it would be a lot cleaner if they could be
combined in one file.


Just be creative:

<?php
//routines.php

switch($routine){
case 'first':
// some code

break;
case 'second':
// some different code

break;
default:
// error control code?
}
?>

<?php
// other.php
function myFunc($var1,$var2){
// code

$routine='second';
include 'routines.php';

// code
}

Now you can do as you want with include a single include file simply by
setting a flag to indicate which routine to use...

--
Justin Koivisto - ju****@koivi.com
http://www.koivi.com
Jul 17 '05 #3
Carved in mystic runes upon the very living rock, the last words of
Alvaro G. Vicario of comp.lang.php make plain:
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):
Is this something that has ever been suggested, discussed, debated
and/or considered? By subroutine I mean a chunk of code that can be
called like a function but which executes in the global space. Yes, I
know an include file executes in the global space (if called from
there), but when you have a number of such chunks of code, it would
be a lot cleaner if they could be combined in one file.


Do you mean functions?


By subroutine I mean a chunk of code that can be called like a function
but which executes in the global space.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #4
Carved in mystic runes upon the very living rock, the last words of
Justin Koivisto of comp.lang.php make plain:
Alan Little wrote:
Is this something that has ever been suggested, discussed, debated
and/or considered?


Just be creative:


Thank you for your feedback. I have thought of a number of ways of
approximating the behavior I'm seeking, including what you suggested, but
nothing is exactly what I want.

For example, right now I'm working on a template processor which is
called as an include file. It uses a number of custom tags in the
template to control its operation. However, the operation of one tag --
we'll call it Tag A -- needs to be duplicated within the operation of
another tag -- Tag B. I can either duplicate the Tag A code within the
Tag B code, or I can have the processor include itself recursively, with
a flag indicating to process just the specific block of the template,
rather than the whole thing. Either approach will work, but neither is as
clean as if I could just put the whole Tag A code into a subroutine and
call it from Tag B. With the second approach my code has to look
something like:

if (!$FlagIsSet) {
(Tag A) {
[Tag A code]
[Set Flag]
[Recursive include for Tag B functionality]
}

(Tag B) {
[Set Flag]
[Recursive include for Tag B functionality]
}

(Tag C) {
[Tag C code]
}
}

else {
[Tag B code]
}

It's just not as clean.

Anyway, I was just wondering if this was something that had been
considered or discussed in the past. I've run into a need for it a number
of times.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #5
On Thu, 03 Mar 2005 17:06:10 -0600, Alan Little <al**@n-o-s-p-a-m-phorm.com>
wrote:
Carved in mystic runes upon the very living rock, the last words of
Alvaro G. Vicario of comp.lang.php make plain:
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):
Is this something that has ever been suggested, discussed, debated
and/or considered? By subroutine I mean a chunk of code that can be
called like a function but which executes in the global space. Yes, I
know an include file executes in the global space (if called from
there), but when you have a number of such chunks of code, it would
be a lot cleaner if they could be combined in one file.


Do you mean functions?


By subroutine I mean a chunk of code that can be called like a function
but which executes in the global space.


I'm having trouble working out what the advantage of this over ordinary
functions with a "global" statement for the variables it uses would be?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6
Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
Justin Koivisto of comp.lang.php make plain:

Alan Little wrote:

Is this something that has ever been suggested, discussed, debated
and/or considered?


Just be creative:

Thank you for your feedback. I have thought of a number of ways of
approximating the behavior I'm seeking, including what you suggested, but
nothing is exactly what I want.

For example, right now I'm working on a template processor which is
called as an include file. It uses a number of custom tags in the
template to control its operation. However, the operation of one tag --
we'll call it Tag A -- needs to be duplicated within the operation of
another tag -- Tag B. I can either duplicate the Tag A code within the
Tag B code, or I can have the processor include itself recursively, with
a flag indicating to process just the specific block of the template,
rather than the whole thing. Either approach will work, but neither is as
clean as if I could just put the whole Tag A code into a subroutine and
call it from Tag B. With the second approach my code has to look
something like:

if (!$FlagIsSet) {
(Tag A) {
[Tag A code]
[Set Flag]
[Recursive include for Tag B functionality]
}

(Tag B) {
[Set Flag]
[Recursive include for Tag B functionality]
}

(Tag C) {
[Tag C code]
}
}

else {
[Tag B code]
}

It's just not as clean.

Anyway, I was just wondering if this was something that had been
considered or discussed in the past. I've run into a need for it a number
of times.

require_once() springs to mind <g>
Jul 17 '05 #7
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 17:06:10 -0600):
By subroutine I mean a chunk of code that can be called like a function
but which executes in the global space.


If you mean a function that can access global variables without previouly
loading them with the "global" keyword (for whatever the reason), I can
think of two approaches:

1) Access variables through the $GLOBALS array.

2) Create a function that reads the $GLOBALS array and creates local
variables. Call that function from the other functions that need it.
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Manda tus dudas al grupo, no a mi buzón
-+ Send your questions to the group, not to my mailbox
--
Jul 17 '05 #8
Carved in mystic runes upon the very living rock, the last words of Andy
Hassall of comp.lang.php make plain:
On Thu, 03 Mar 2005 17:06:10 -0600, Alan Little
<al**@n-o-s-p-a-m-phorm.com> wrote:
Carved in mystic runes upon the very living rock, the last words of
Alvaro G. Vicario of comp.lang.php make plain:
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 16:08:19 -0600):

Is this something that has ever been suggested, discussed, debated
and/or considered? By subroutine I mean a chunk of code that can be
called like a function but which executes in the global space. Yes,
I know an include file executes in the global space (if called from
there), but when you have a number of such chunks of code, it would
be a lot cleaner if they could be combined in one file.

Do you mean functions?


By subroutine I mean a chunk of code that can be called like a
function but which executes in the global space.


I'm having trouble working out what the advantage of this over
ordinary functions with a "global" statement for the variables it uses
would be?


When the code doesn't know what variables it will be using.

The best example is probably the template processor I'm working on right
now. The template contains placeholders for variables; the processor
needs to replace them with the appropriate value. It doesn't know until
it reads the template what variables it needs. I understand this isn't a
difficult problem; as I said, I can think of a number of ways to do it.
But they just aren't as clean as code that could simply execute in the
space it's called from.

The need for this functionality is obviously already recognized, as it's
basically what an include file is: centralized code that can be called
from multiple locations and executes in the space it's called from. What
I'm proposing is to not require a separate file for each such chunk of
code.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #9
Alan Little wrote:
Is this something that has ever been suggested, discussed, debated and/or
considered? By subroutine I mean a chunk of code that can be called like a
function but which executes in the global space. Yes, I know an include
file executes in the global space (if called from there), but when you have
a number of such chunks of code, it would be a lot cleaner if they could be
combined in one file.
and...
The need for this functionality is obviously already recognized, as it's
basically what an include file is: centralized code that can be called
from multiple locations and executes in the space it's called from. What
I'm proposing is to not require a separate file for each such chunk of
code.
The more you have to include a file to do what you are after, the bigger
your total script gets... not what is intended by functions, and not a
neat solution, as you say.

The best example is probably the template processor I'm working on right
now. The template contains placeholders for variables; the processor
needs to replace them with the appropriate value. It doesn't know until
it reads the template what variables it needs. I understand this isn't a
difficult problem; as I said, I can think of a number of ways to do it.
But they just aren't as clean as code that could simply execute in the
space it's called from.

Just in case you didn't know this - you can use GLOBAL with a variable:

eg:

$k = 'a';
global ${$k};

will give you access to the global 'a'...

So you can do what you want.

But I agree with your wanting to be able to access the global space
more easilly.

As much as I don't like globals, PHP is an interpreted script, and under
web usage, runs once to complete a single job. So I feel there is an
argument in favour of being pratical over writing 'perfect computer
science' code.

Globals have their place and use :-

I want to use constant defines (or thats what they would be in C),
rather than using a number throughout my code - something which is
considered good pratice.

But I don't want to have to 'GLOBAL' 10 constant values in every
function that uses them...

My suggestion is to add a 'SUPERGLOBAL' operator, that you can use
in the global space to add a variable to the superglobal list, eg:

SUPERGLOBAL $my_global_a = 1;

and then you don't need to do the 'GLOBAL $my_global_a' in every
function...

John.

Jul 17 '05 #10
.oO(John)
Globals have their place and use :-

I want to use constant defines (or thats what they would be in C),
rather than using a number throughout my code - something which is
considered good pratice.

But I don't want to have to 'GLOBAL' 10 constant values in every
function that uses them...


Then why don't you use constants?

Micha
Jul 17 '05 #11
On Fri, 4 Mar 2005 15:02:05 +0100, "Alvaro G. Vicario"
<kA*****************@terra.es> wrote:
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 17:06:10 -0600):
By subroutine I mean a chunk of code that can be called like a function
but which executes in the global space.


If you mean a function that can access global variables without previouly
loading them with the "global" keyword (for whatever the reason), I can
think of two approaches:

1) Access variables through the $GLOBALS array.

2) Create a function that reads the $GLOBALS array and creates local
variables. Call that function from the other functions that need it.


With the EXTR_REFS option to extract(), can you get very close to
automatically creating local variables that are each references to the
equivalent global variable by running extract() against the $GLOBALS array? If
this works, it ought to result in something nearly indistinguishable from being
in the outer scope?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #12
Michael Fesser wrote:
.oO(John)

Globals have their place and use :-

I want to use constant defines (or thats what they would be in C),
rather than using a number throughout my code - something which is
considered good pratice.

But I don't want to have to 'GLOBAL' 10 constant values in every
function that uses them...

Then why don't you use constants?


Sorry, my mistake - thanks for the tip, will do...

But I still like the idea of user defined superglobal's.

John.

Jul 17 '05 #13
Carved in mystic runes upon the very living rock, the last words of Andy
Hassall of comp.lang.php make plain:
On Fri, 4 Mar 2005 15:02:05 +0100, "Alvaro G. Vicario"
<kA*****************@terra.es> wrote:
*** Alan Little escribió/wrote (Thu, 03 Mar 2005 17:06:10 -0600):
By subroutine I mean a chunk of code that can be called like a
function but which executes in the global space.


If you mean a function that can access global variables without
previouly loading them with the "global" keyword (for whatever the
reason), I can think of two approaches:

1) Access variables through the $GLOBALS array.

2) Create a function that reads the $GLOBALS array and creates local
variables. Call that function from the other functions that need it.


With the EXTR_REFS option to extract(), can you get very close to
automatically creating local variables that are each references to the
equivalent global variable by running extract() against the $GLOBALS
array?


Yes, that works. Thank you. I was concerned it would only change the
value in the array itself, but apparently variables in the global space
are themselves references to the $GLOBALS array. I don't believe this was
always the case; I seem to recall testing this once long ago, and
changing $GLOBALS didn't change the variable value.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
Jul 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by joe | last post: by
3 posts views Thread by OpticTygre | last post: by
5 posts views Thread by Victor | last post: by
1 post views Thread by Wijaya Edward | last post: by
compman9902
5 posts views Thread by compman9902 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.