470,614 Members | 1,584 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

Calling a function defined within a funcion

Hi,

This may be the dumbest question ever, but in the following code (using
PHP 4.3.8) how do I call b() from within a()?

class test {
function a() {
print "a called";
b();

function b() {
print "b called";
}
}
}

using "b()" (as in the code above) doesn't work, neither does $this->b()
or test::b(), so how do I call it?

Thanks!

Andrew
Jul 17 '05 #1
3 2264
Andrew Boothman wrote:
Hi,

This may be the dumbest question ever, but in the following code (using
PHP 4.3.8) how do I call b() from within a()?

class test {
function a() {
print "a called";
b();

function b() {
print "b called";
}
}
}

using "b()" (as in the code above) doesn't work, neither does $this->b()
or test::b(), so how do I call it?


You will need to define the function before calling it.

Regards,

Per
Jul 17 '05 #2
"Andrew Boothman" <an*************@blueyonder.co.uk> wrote in message
news:JF***********************@news-text.cableinet.net...
This may be the dumbest question ever, but in the following code (using
PHP 4.3.8) how do I call b() from within a()?

class test {
function a() {
print "a called";
b();

function b() {
print "b called";
}
}
}

using "b()" (as in the code above) doesn't work, neither does $this->b()
or test::b(), so how do I call it?


first, you would need to define function b prior to calling it. Even when
you would do so, than you would be able to call function a only once, since
second call to a would try to define function b() which would be by that
time allready defined.

In php there is no notion of local function, as you would know it from other
languages like Pascal. The construct you are using might misslead you, that
you are defining function b local to function, but you are not, you are just
within code of a executing statement that in effect defines global function
b().

rush
--
http://www.templatetamer.com/
Jul 17 '05 #3
Per Gustafsson wrote:
Andrew Boothman wrote:
Hi,

This may be the dumbest question ever, but in the following code (using
PHP 4.3.8) how do I call b() from within a()?

class test {
function a() {
print "a called";
b();

function b() {
print "b called";
}
}
}

using "b()" (as in the code above) doesn't work, neither does $this->b()
or test::b(), so how do I call it?


You will need to define the function before calling it.


No, the function needs to be defined before you can call it - that's not the
same thing at all - unlike 'C', PHP will quite happily call functions
defined below its invocation in the source file.

To call a METHOD within an OBJECT, you need to instantiate the object and
reference the method e.g.

class test {
function a() {
print "a called";
$this->b(); // ********

function b() {
print "b called";
}
}
}
using "b()" (as in the code above) doesn't work, neither does $this->b()
or test::b(), so how do I call it?


I guess you didn't create an instance -

$testObj = new test;
$testObj->a();

HTH

C.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by joseluismarchetti | last post: by
4 posts views Thread by Zeebra3 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.