473,786 Members | 2,672 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 1496
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.on et.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
3280
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
3
1632
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 that function without having to create a huge SELECT CASE statement. i.e. Node.123's data is created in a function called "fnGetProjectDetails"
3
1579
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
5906
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 route. The problem being that I can't call these functions from the query designer in Access. I decided that I would try the route of declaring the functions from the dll file the same way you would for the Windows API. Access then complains that...
10
2321
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 call to work? Exists is a method within the Swatch class NOT the SwatchPanel class. Is this possible? Suggestions would be very welcome.
7
2828
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 do it?
6
3623
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 sending the Sandal type to the base class I can not instantiate a Sandal class from within Shoe. Public Class Shoe Public Shared Function Load() As Object return ?????
3
2082
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 several Ajax.Request calls to gather data from the server. What I'm having trouble with is getting the data from the Ajax call back to the custom class instance. I basicially want to get a Javascript array from my PHP page and insert that into the...
9
4724
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
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.