php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29104 Function declaration in method doesn't work
Submitted: 2004-07-12 09:41 UTC Modified: 2005-04-27 15:31 UTC
Votes:5
Avg. Score:3.4 ± 1.5
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:3 (75.0%)
From: tomas_matousek at hotmail dot com Assigned: andi
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-03-06 OS: *
Private report: No CVE-ID:
 [2004-07-12 09:41 UTC] tomas_matousek at hotmail dot com
Description:
------------
Declaration of a function in a method doesn't work.
IMHO by declaring a function in a method one creates a function not a method. It it was a method it would be possible to call it by $a->f(). That works neither.

See the code below:

Reproduce code:
---------------
class A
{ 
  function g() 
  { 
    echo "function g - begin\n";
    
    function f() 
    { 
      echo "function f\n";
    } 
    
    echo "function g - end\n";
  }
}

$a = new A;
$a->g();
f();            

Expected result:
----------------
function g - begin
function g - end
function f

Actual result:
--------------
function g - begin
function g - end

Fatal error:  Non-static method A::f() cannot be called statically ...



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-13 17:45 UTC] Jason at hybd dot net
From what I gather, like most languages, PHP doesn't 
support 'nested' methods. (And therefore I doubt this is 
a bug)

Where you are calling f() is ambigous. As far as PHP is 
concerned f() is probably a global function and not a 
method embedded inside a::g().
 [2004-07-13 21:42 UTC] tomas_matousek at hotmail dot com
PHP supports declaring functions "inline", i.e. almost anywhere in a code. Such function is declared as global wherever it is declared. Hence, I don't see any ambiguity if a function is declared inside a method. PHP doesn't support adding new methods into existing class. That's why a function declared in method can only be a global one.
 [2004-07-13 21:46 UTC] helly@php.net
PHP does not support nested functions. Still we need to disable this.
 [2004-07-16 18:00 UTC] postings-php-bug at hans-spath dot de
Wait a minute, PHP doesn't support nested functions?

Holy shit, documentation lies!

http://www.php.net/manual/en/language.functions.php
"Example 12-3. Functions within functions"

Do we need to file a documentation bug, too?
 [2004-08-14 01:24 UTC] andrey@php.net
While nested functions are maybe useful feature for someone declaration of a function inside the body of a method (which happens to be a function inside a class) is _ambigious_ . Why? There is no reserved word "method" for marking methods of a class and "function" is used so when it is between {} after class name "function" creates a method of the class. IMO "function" inside a method should not be possible.
 [2004-12-30 15:16 UTC] ulderico at maber dot com dot br
IMHO, Nested Functions are BAD&WRONG, thus they should be disabled. 

Firstly, when you DECLARE a function inside a function, you have a redeclaration problem. Try to execute the parent function twice and most likely you'll receive a message: "Fatal error:  Cannot redeclare XXXX". 

OK! Some may dispute: "let's create an undeclare_function() so as to allow at the end of the function undeclare the child function. It would enable to reinvoke the parent function whenever we like". Well, THIS IS ALSO B&R. Why would you undeclare a function that you're going to use?

Secondly, if a function needs to work in a closed (encapsuled) environment, well, I think you need a CLASS, not a function. In a class you may have a public, private or protected variables invoked by either public, private or protected methods.

Thusly, a code like this (sorry the indentation, I want to save space):
class A
{ 
  function b(){}
  function c(){}
  function d(){}
  function g(){ 
    echo "function g - begin\n";
    function f(){echo "function f\n";} 
    echo "function g - end\n";
  }
}

should be written like this:
class A
{ 
  function b(){}
  function c(){}
  function d(){}
  function g(){ 
    echo "function g - begin\n";
    f::f();
    echo "function g - end\n";
  }
}
class f{
    function f(){echo "function f\n";} 
}

$obj = new A();
$obj->g();

So, the rationale is, why you need to have function within function if you've got classes?
 [2004-12-30 15:29 UTC] ulderico at maber dot com dot br
OH! In time! Just to reinforce the first and the second paragraph of my last comment.

Why would you create a function that should be invoked JUST ONCE? Initialize environment? It makes no point. You can do it directly in the code, using "if" to distinguish any situation of environment that one may have.
 [2005-03-06 20:39 UTC] sniper@php.net
Assigned to the mastah..


 [2005-03-25 15:14 UTC] php at trancer dot nl
Mind you that such code _IS_ working PHP4 so it is breaking BC and not listed on http://www.zend.com/manual/migration5.incompatible.php 

As to why it could be useful.. to do recursion and not register a function in global scope. (Of course there are methods for this aswell.)
 [2005-04-27 15:31 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_0.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 10:02:09 2014 UTC