Call class function from within another function?

This is a discussion on Call class function from within another function? within the PHP Language forums, part of the PHP Programming Forums category; Hey fellow nerds, Take this code for example: class hello { function world() { echo "Hello"; } } $hello = new hello(); function ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-04-2008
robbiesmith79
 
Posts: n/a
Default Call class function from within another function?

Hey fellow nerds,

Take this code for example:

class hello {
function world() {
echo "Hello";
}
}

$hello = new hello();

function writeHelloWorld() {
$hello->world();
}

writeHelloWorld();

I get this error

Fatal error: Call to a member function on a non-object in (points to
"$hello->world();" line)

Why is this?

Robbie
Reply With Quote
  #2 (permalink)  
Old 05-04-2008
Paul Lautman
 
Posts: n/a
Default Re: Call class function from within another function?

robbiesmith79 wrote:
> Hey fellow nerds,
>
> Take this code for example:
>
> class hello {
> function world() {
> echo "Hello";
> }
> }
>
> $hello = new hello();
>
> function writeHelloWorld() {
> $hello->world();
> }
>
> writeHelloWorld();
>
> I get this error
>
> Fatal error: Call to a member function on a non-object in (points to
> "$hello->world();" line)
>
> Why is this?
>
> Robbie


Because $hello is not in scope within the function writeHelloWorld


Reply With Quote
  #3 (permalink)  
Old 05-04-2008
Piotr
 
Posts: n/a
Default Re: Call class function from within another function?

robbiesmith79 wrote:
> Hey fellow nerds,
>
> Take this code for example:
>
> class hello {
> function world() {
> echo "Hello";
> }
> }
>
> $hello = new hello();
>
> function writeHelloWorld() {
> $hello->world();
> }
>
> writeHelloWorld();
>
> I get this error
>
> Fatal error: Call to a member function on a non-object in (points to
> "$hello->world();" line)
>
> Why is this?
>
> Robbie


It's bad scope.
You could do (bad way):
--------------------------------

$hello = new hello();

function writeHelloWorld() {
global $hello;
$hello->world();
}

--------------------------------

or (good way):

--------------------------------

$hello = new hello();

function writeHelloWorld($hello) {
$hello->world();
}

writeHelloWorld($hello);
--------------------------------
best regaards
Piotr N
Reply With Quote
  #4 (permalink)  
Old 05-04-2008
robbiesmith79
 
Posts: n/a
Default Re: Call class function from within another function?

Fantastic!
Reply With Quote
  #5 (permalink)  
Old 05-04-2008
Thomas Mlynarczyk
 
Posts: n/a
Default Re: Call class function from within another function?

Piotr schrieb:

> $hello = new hello();
>
> function writeHelloWorld($hello) {
> $hello->world();
> }


// Typehint to make sure
// that method world() is available
function writeHelloWorld( hello $hello )
{
$hello->world();
}

Greetings,
Thomas


--
Ce n'est pas parce qu'ils sont nombreux Ã* avoir tort qu'ils ont raison!
(Coluche)
Reply With Quote
  #6 (permalink)  
Old 05-05-2008
Rik Wasmus
 
Posts: n/a
Default Re: Call class function from within another function?

On Sun, 04 May 2008 19:03:40 +0200, Piotr <spam@poczta.onet.pl> wrote:

> robbiesmith79 wrote:
>> Hey fellow nerds,
>> Take this code for example:
>> class hello {
>> function world() {
>> echo "Hello";
>> }
>> }
>> $hello = new hello();
>> function writeHelloWorld() {
>> $hello->world();
>> }
>> writeHelloWorld();
>> I get this error
>> Fatal error: Call to a member function on a non-object in (points to
>> "$hello->world();" line)
>> Why is this?
>> Robbie

>
> It's bad scope.
> You could do (bad way):
> --------------------------------
>
> $hello = new hello();
>
> function writeHelloWorld() {
> global $hello;
> $hello->world();
> }
>
> --------------------------------
>
> or (good way):
>
> --------------------------------
>
> $hello = new hello();
>
> function writeHelloWorld($hello) {
> $hello->world();
> }
>
> writeHelloWorld($hello);
> --------------------------------



Either that, or depending on the need:

class hello{
static function world(){
echo "Hello";
}
}
function writeHelloWorld(){
hello::world();
}
--
Rik Wasmus
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:31 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0