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