Calling a function defined within a funcion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Boothman

    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
  • Per Gustafsson

    #2
    Re: Calling a function defined within a funcion

    Andrew Boothman wrote:[color=blue]
    > 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?
    >[/color]

    You will need to define the function before calling it.

    Regards,

    Per

    Comment

    • rush

      #3
      Re: Calling a function defined within a funcion

      "Andrew Boothman" <andrew-boothman@blueyo nder.co.uk> wrote in message
      news:JFjPc.1281 8$LF5.131905656 @news-text.cableinet. net...[color=blue]
      > 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?[/color]

      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
      --



      Comment

      • Colin McKinnon

        #4
        Re: Calling a function defined within a funcion

        Per Gustafsson wrote:
        [color=blue]
        > Andrew Boothman wrote:[color=green]
        >> 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?
        >>[/color]
        >
        > You will need to define the function before calling it.
        >[/color]

        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";
        }
        }
        }
        [color=blue][color=green]
        >> using "b()" (as in the code above) doesn't work, neither does $this->b()
        >> or test::b(), so how do I call it?[/color][/color]

        I guess you didn't create an instance -

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

        HTH

        C.

        Comment

        Working...