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

Can a function be called from a function?

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Bruce W...1" <br***@noDirectEmail.com> wrote in message news:<3F***************@noDirectEmail.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
Jul 17 '05 #2

P: n/a
yes, in any language
"R. Rajesh Jeba Anbiah" <ng**********@rediffmail.com> wrote in message
news:ab**************************@posting.google.c om...
| "Bruce W...1" <br***@noDirectEmail.com> wrote in message
news:<3F***************@noDirectEmail.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
Jul 17 '05 #3

P: n/a
Seems to be ok except that there is no constructor in your class.

.b

Bruce W...1 wrote:
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?


Jul 17 '05 #4

P: n/a
$greeting = new Nice() needs a ;

$greet = $this->sayHi();

like this, the code works for me, even though there is still no
constructor.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.