473,226 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 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 7201
*** 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: joe | last post by:
Hello: I am using IIS 5.0, ASP and VBScript. I am puzzled by recurrences of errors when I nest subroutines, and I'd like to know if there are rules for that which I don't know. Would functions...
7
by: Microsoft | last post by:
I'm not sure where to physically place my subroutines in vb.net I get namespace and not declared errors... Imports System Imports System.Management Public Class Form1
3
by: OpticTygre | last post by:
I have a class, ProcessFiles, with several subroutines it runs for each type of file I want to "process." First, I check directories for files. Then, based on the filenames of those I find in...
5
by: Victor | last post by:
In ASP, if I define a subroutine as private Private Sub mySubroutine(arg1, arg2) I understand that the variables I define inside the subroutine are local only to the subroutine. If the same...
1
by: Wijaya Edward | last post by:
Hi, How does one benchmark multiple subroutines in Python? Is there a built-in library for that? -- Regards, Edward WIJAYA SINGAPORE
1
by: hanhaner | last post by:
hey, there I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it. ...
0
by: hanhaner | last post by:
hey, there I am new to Python and C++. Now I am trying to call some fortran subroutines in python code. I was told the good way is to write a c++ interface. I am not quite sure how to do it. ...
5
compman9902
by: compman9902 | last post by:
Hello, and thank you for viewing this thread. I was just wondering if there was a way to have subroutines, like in BASIC, in C++ that can use all of the same variables that main() does.
0
by: bbasberg | last post by:
Hello, First of all, I'm very sorry i mistakenly posted this message to the "articles" forum rather than this one and am unable to erase it from there. I apologize for the cross-posting. ...
10
by: liz0001 | last post by:
Hi, I need to access an .asp subroutine file from a subdomain. i.e. the website is at www.domain.com and I want to access the same subroutines on the subdomain mobile.domain.com. The...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.