|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-06-15 18:07 UTC] gary at conduit-it dot com
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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 22:00:01 2025 UTC |
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. - JamesHere'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);