470,815 Members | 1,098 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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
Jun 2 '08 #1
5 1351
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
Jun 2 '08 #2
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
Jun 2 '08 #3
Fantastic!
Jun 2 '08 #4
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)
Jun 2 '08 #5
On Sun, 04 May 2008 19:03:40 +0200, Piotr <sp**@poczta.onet.plwrote:
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
Jun 2 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by enfis.the.paladin | last post: by
7 posts views Thread by Rafał Maj Raf256 | last post: by
6 posts views Thread by roland.bali | last post: by
9 posts views Thread by Andrew Poulos | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.