By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

Calling a function defined within a funcion

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a
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.