ID: 11507
User Update by: [EMAIL PROTECTED]
Status: Open
Bug Type: Feature/Change Request
Operating system: FreeBSD
PHP Version: 4.0.5
Description: Function within a function namespace
Here's another simple example that causes a parse error.
function add_number($num)
{
function add_one($num)
{
$num++;
return($num);
}
$num = add_one($num);
return($num);
}
echo add_number(0);
echo add_number(1);
Previous Comments:
---------------------------------------------------------------------------
[2001-06-18 23:58:08] [EMAIL PROTECTED]
My point here is that a function's name called within a function should remain
entirely within that scope. The concept is similarly supported here the documentation
for the include() function:
"When a file is include()ed, the code it contains inherits the variable scope of the
line on which the include() occurs. Any variables available at that line in the
calling file will be available within the called file. If the include() occurs inside
a function within the calling file, then all of the code contained in the called file
will behave as though it had been defined inside that function."
(http://www.php.net/manual/en/function.include.php)
I realize this is mostly in reference to variables and not directly about functions,
but shouldn't the same rule hold true for both? Functions within a class do not cause
this problem. They remain within scope of the class. I can have functions of the
same name in different classes, or as a result of multiple instances of the same
class. Shouldn't functions within another function do the same?
Maybe my example wasn't clear. Our three goals:
1) Include files that are self-contained "snippets" of php and html. The are
displayed as blocks or elements of a web page. Ie. navigation, headers, footers,
picture w/ caption, list of events�.
2) The included files (design elements) can be programmed with limited experience and
minimum complexity for more compact, easier, and faster coding. Typically limited to
simple functions, basic control structures, and database selects via an API.
Functions specific to the creation of the page's design and layout are coded in this
include file. Core functions (no design output) are already included and globally
available.
3) The include file may be included one or more times on a page.
For instance, if you where to have a web page with a cookie-crumb navigation bar at
the top and at the bottom, any functions that were part of the include file and helped
to drill down the category structure would cause an error the second time the
navigation file was included. This is despite the fact that in each case when the
file was included, it was inside the scope of a function. I've even gone so far as to
abstract the function one more scope-layer by adding a class around it for each time
any element on the page is included. In other words I call an instance of a class
passing the ElementID which then calls the constructor. The constructor calls a
function in the class passing the ElementID, which then includes the file that has the
design functions in it.
The alternative is to create each element include file as a class. Before generating
the page, the system would have to first take one loop through the elements that will
appear on the page to include the relevant classes. It may then have to make a second
loop to actually generate the page by creating instances of the classes each time they
are to be displayed. In this case the structure of the class won't really be taken
advantage of. The person programming the element file will have to be more
experienced. The database driving the system will have to keep more information about
each of the elements in order to call instances of the classes. And, the system may
essentially have to take two loops through the page to generate it.
I don't know how else to impress the stop-gap this actually causes. For our project,
this may substantially change the performance of our application. Out goal to create
an easy to implement API for our content management module of our e-business
application may be unachievable. I truly hope you give this serious consideration.
Thanks,
Gary M. Cornelisse
Senior Applications Programmer
Conduit Internet Technologies, Inc.
[EMAIL PROTECTED]
800.493.5045 x206
814.867.8248 fax
Conduit-IT: Powering Successful eBusiness
http://www.conduit-it.com/
---------------------------------------------------------------------------
[2001-06-17 05:15:17] [EMAIL PROTECTED]
Why not do somthing sensible like call a function that creates an array of callback
references:
so
function blah() {
$foo = array();
$foo["render"] = "bar";
}
$foo["render"]() would call bar();
There is no mention of function namespaces in PHP's documentation at all and we have
never really seen the need for them. Changing to a feature/change request.
- James
---------------------------------------------------------------------------
[2001-06-15 18:07:01] [EMAIL PROTECTED]
I have found that a function within a function takes a global namespace. Shouldn't
the inside function declaration remain inside the scope of the outer function? For
example:
function add_number($num)
{
function add_one($num)
{
$num++;
return($num);
}
$num = add_one($num);
if($num <= 10)
{
$num = add_number($num);
}
return($num);
}
I know, why would anyone want to do this? I'll try to explain how I came about this.
We're writing a content management application. What I'm trying to do is write a
function that includes a file that has a function in it. The file may be included
more than once on the page depending on the designer's preference. Say we have an
include file that generates a "navigation block" on the page. It has a function
specific to doing that task. The nav-block may not appear on every page, so it's
inefficient to include the function unless I'm going to generate the nav-block. I
therefore have to provide the function in the include file along with the html and
other php code to generate the nav-block when and where I want. I wrote a function
which I pass an ID to, which then includes the nav-block file. But, if I try to put
another copy of that same nav-block on the same page, I get the "Cannot redeclare..."
error. Yes, I could use a class structure, but we are trying to achieve a way of
programming these "blocks" in the most simplistic way by minizing the amount of php
knowledge necessary.
If any of this is not clear, I'm willing to go to any length to explain and to find
out if this can be resolved. It's pretty important that we can do this. One of the
main goals to our software depends on this.
Thanks
---------------------------------------------------------------------------
Full Bug description available at: http://bugs.php.net/?id=11507
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]