Can a function be called from a function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce W...1

    Can a function be called from a function?

    Can a function in a class be called from another function? I create a
    class object and call a method which calls another method in the same
    class and I get erors.

    class Nice
    {
    function getHi()
    {
    $greet = sayHi();
    return $greet;
    }

    function sayHi()
    {
    return "Hi there from db.";
    }
    }

    $greeting = new Nice()
    $test = $greeting->getHi();
    echo $test;

    Fatal error: Call to undefined function: sayhi()

    And yes I tried ->
    $greet = this->sayHi(); too.

    Why woun't this work? Or is this beyond the abilities of the PHP
    scripting language?

    Thanks for your help?
  • R. Rajesh Jeba Anbiah

    #2
    Re: Can a function be called from a function?

    "Bruce W...1" <bruce@noDirect Email.com> wrote in message news:<3F8CB526. 9AF2D560@noDire ctEmail.com>...[color=blue]
    > Can a function in a class be called from another function? I create a
    > class object and call a method which calls another method in the same
    > class and I get erors.
    >
    > class Nice
    > {
    > function getHi()
    > {
    > $greet = sayHi();
    > return $greet;
    > }
    >
    > function sayHi()
    > {
    > return "Hi there from db.";
    > }
    > }
    >
    > $greeting = new Nice()
    > $test = $greeting->getHi();
    > echo $test;
    >
    > Fatal error: Call to undefined function: sayhi()
    >
    > And yes I tried ->
    > $greet = this->sayHi(); too.[/color]
    ^^^^^
    $this->sayHi();

    Always, manual is your friend.

    ---
    Our songs have meaning for everyone. I don't know of anyone who
    isn't either in love, just out of love, or else wants to be in love.
    --- Graham Russell, Air Supply
    Email: rrjanbiah-at-Y!com

    Comment

    • s a n j a y

      #3
      Re: Can a function be called from a function?

      yes, in any language


      "R. Rajesh Jeba Anbiah" <ng4rrjanbiah@r ediffmail.com> wrote in message
      news:abc4d8b8.0 310142041.245be c52@posting.goo gle.com...
      | "Bruce W...1" <bruce@noDirect Email.com> wrote in message
      news:<3F8CB526. 9AF2D560@noDire ctEmail.com>...
      | > Can a function in a class be called from another function? I create a
      | > class object and call a method which calls another method in the same
      | > class and I get erors.
      | >
      | > class Nice
      | > {
      | > function getHi()
      | > {
      | > $greet = sayHi();
      | > return $greet;
      | > }
      | >
      | > function sayHi()
      | > {
      | > return "Hi there from db.";
      | > }
      | > }
      | >
      | > $greeting = new Nice()
      | > $test = $greeting->getHi();
      | > echo $test;
      | >
      | > Fatal error: Call to undefined function: sayhi()
      | >
      | > And yes I tried ->
      | > $greet = this->sayHi(); too.
      | ^^^^^
      | $this->sayHi();
      |
      | Always, manual is your friend.
      |
      | ---
      | Our songs have meaning for everyone. I don't know of anyone who
      | isn't either in love, just out of love, or else wants to be in love.
      | --- Graham Russell, Air Supply
      | Email: rrjanbiah-at-Y!com


      Comment

      • odel

        #4
        Re: Can a function be called from a function?

        Seems to be ok except that there is no constructor in your class.

        .b

        Bruce W...1 wrote:
        [color=blue]
        > Can a function in a class be called from another function? I create a
        > class object and call a method which calls another method in the same
        > class and I get erors.
        >
        > class Nice
        > {
        > function getHi()
        > {
        > $greet = sayHi();
        > return $greet;
        > }
        >
        > function sayHi()
        > {
        > return "Hi there from db.";
        > }
        > }
        >
        > $greeting = new Nice()
        > $test = $greeting->getHi();
        > echo $test;
        >
        > Fatal error: Call to undefined function: sayhi()
        >
        > And yes I tried ->
        > $greet = this->sayHi(); too.
        >
        > Why woun't this work? Or is this beyond the abilities of the PHP
        > scripting language?
        >
        > Thanks for your help?[/color]

        Comment

        • odel

          #5
          Re: Can a function be called from a function?

          $greeting = new Nice() needs a ;

          $greet = $this->sayHi();

          like this, the code works for me, even though there is still no
          constructor.

          Comment

          Working...