473,511 Members | 14,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1475
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3252
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
3
1609
by: Supa Hoopsa | last post by:
I am using a treeview and within each node, I want to store the name of the function that was called to create the node's data. Then whenever a user selects this node, I want to be able to call...
3
1572
by: enfis.the.paladin | last post by:
Hi to all! I have something like this: class FWrap { public: virtual void READ (void) = 0; } class Optimized { private:
7
5877
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
10
2294
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
7
2799
by: Rafał Maj Raf256 | last post by:
There was a way to trinck program to call purly abstract non-existing method (causing undefined behaviour), I would like to try that for curiosity (how different compilers/platforms react), how to...
6
3613
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
3
2055
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes...
9
4700
by: Andrew Poulos | last post by:
Say I have this: var foo = function(x, y) { return x + y; }; var bar = function(fname, param1, param2) { /* * how do I call the function "foo" * and pass parameters to it?
0
7237
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.