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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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) |
|
|||
|
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 |