PDA

View Full Version : function in another function


piz
12-14-2002, 11:47 AM
Hi,

As far as I know its possible to define a function in another function.

Is this possible in Classes, too?

This Script returns following error:
Fatal error: Call to undefined function: getv() in F:\www.united-scripts.com\class_php.php on line 17

$test = new testclass(1);
echo $test->testarray[0];

class testclass
{
var $testarray = array();

function testclass($v)
{
$this->testarray[] = getv($v);

function getv($vv)
{
return $vv++;
}
}

}


Saludo, piz

mordred
12-14-2002, 11:57 AM
Try to first define the function and later call it:


$test = new testclass(5);
echo $test->testarray[0];

class testclass
{
var $testarray = array();

function testclass($v)
{
function getv($vv)
{
return ++$vv;
}

$this->testarray[] = getv($v);
}

}


Works for me in PHP4.1.2... and I'm quite surprised that it actually works...

P.S: I had to change the post-increment to pre-increment.

Kiwi
12-14-2002, 01:00 PM
It's possible to nest functions, but there's not really much point. All functions are global, irrespective of nesting.

It might be helpful for you to see what's going on in your code, but there's no other reason to do it.