473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

What is wrong with this code?

Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";

Test($Message)
{
$this->$Message=$Message;
}

Display_Message()
{
echo "$Message";
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>
Thanks in advance for your help.
Jul 17 '05 #1
10 1452
siu02rk wrote:
Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";

Test($Message)
{
$this->$Message=$Message;
change to:
$this->Message=$Message;
}

Display_Message()
{
echo "$Message";
change to:
echo $this->Message;
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>
Thanks in advance for your help.

--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #2
Justin Koivisto wrote:
siu02rk wrote:
Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";
Test($Message)
(Missed some)
change to:
function Test($Message)
{
$this->$Message=$Message;

change to:
$this->Message=$Message;
}

Display_Message()
change to:
function Display_Message()
{
echo "$Message";

change to:
echo $this->Message;
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>
Thanks in advance for your help.


--
Justin Koivisto - sp**@koivi.com
http://www.koivi.com
Jul 17 '05 #3
*** siu02rk escribió/wrote (2 Sep 2004 13:22:49 -0700):
I am trying to get an understanding of classes within PHP. What is
wrong with this code:
Some quick remarks...
<?php
class Test
{
$Message = "Default Message";
var $Message='Default Message';

Test($Message)
{
$this->$Message=$Message;
}
$this->Message=$Message;


Display_Message()
{
echo "$Message";
}
echo $this->Message;
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>


$TestObject->Display_Message();

--
--
-+ Álvaro G. Vicario - Burgos, Spain - ICQ 46788716
+- http://www.demogracia.com (la web de humor para mayores de 100 años)
++ «Sonríe, que te vamos a hacer una foto para la esquela»
--
Jul 17 '05 #4
siu02rk wrote:
I am trying to get an understanding of classes within PHP. What is
wrong with this code:
[...] <?php
class Test
{
$Message = "Default Message";
In PHP 4 this should read:

var $Message = "...";

In PHP 5:

public/private/protected $Message = "...";

e.g.:

private $Message = "...";

Test($Message)
{
$this->$Message=$Message;
}

This should read:

function Test($Message) {
$this->Message=$Message;
}
Display_Message()
This should read:

function Display_Message()
Thanks in advance for your help.


Welcome. But now it's time to help yourself by starting to read the
following manual pages carefully:

http://www.php.net/manual/en/language.oop.php
JW

Jul 17 '05 #5
"siu02rk" <si*****@hotmail.com> wrote in message
news:82**************************@posting.google.c om...
Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";

Test($Message)
{
$this->$Message=$Message;
}

Display_Message()
{
echo "$Message";
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();


change to:
$TestObject->Display_Message(); // (capitalization error)
Jul 17 '05 #6
On Thu, 02 Sep 2004 22:23:29 GMT, "kingofkolt"
<je**********@comcast.net> wrote:
"siu02rk" <si*****@hotmail.com> wrote in message
news:82**************************@posting.google. com...
Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";

Test($Message)
{
$this->$Message=$Message;
}

Display_Message()
{
echo "$Message";
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();


change to:
$TestObject->Display_Message(); // (capitalization error)

And, less importantly, change </test> to </title>
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================
Jul 17 '05 #7
Gary L. Burnore <gb******@databasix.com> wrote in message news:<ch**********@blackhelicopter.databasix.com>. ..
On Thu, 02 Sep 2004 22:23:29 GMT, "kingofkolt"
<je**********@comcast.net> wrote:
"siu02rk" <si*****@hotmail.com> wrote in message
news:82**************************@posting.google. com...
Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";

Test($Message)
{
$this->$Message=$Message;
}

Display_Message()
{
echo "$Message";
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();


change to:
$TestObject->Display_Message(); // (capitalization error)

And, less importantly, change </test> to </title>
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================== =========================
Want one? GET one! http://signup.databasix.com
================================================== =========================


Hi All,

My code is now :

<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
var $Message = "Default Message";

function Test($Message)
{
$this->Message=$Message;
}

function Display_Message()
{
this->echo Message;
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>

But it still does not work!! Can anyone help? Thanks again for your help.
Jul 17 '05 #8
siu02rk wrote:
Gary L. Burnore <gb******@databasix.com> wrote in message news:<ch**********@blackhelicopter.databasix.com>. ..
On Thu, 02 Sep 2004 22:23:29 GMT, "kingofkolt"
<je**********@comcast.net> wrote:

"siu02rk" <si*****@hotmail.com> wrote in message
news:82**************************@posting.googl e.com...

Hi all,

I am trying to get an understanding of classes within PHP. What is
wrong with this code:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
$Message = "Default Message";

Test($Message)
{
$this->$Message=$Message;
}

Display_Message()
{
echo "$Message";
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();

change to:
$TestObject->Display_Message(); // (capitalization error)


And, less importantly, change </test> to </title>
--
gburnore@databasix dot com
---------------------------------------------------------------------------
How you look depends on where you go.
---------------------------------------------------------------------------
Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³
| ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
Black Helicopter Repair Svcs Division | Official Proof of Purchase
================================================ ===========================
Want one? GET one! http://signup.databasix.com
================================================ ===========================



Hi All,

My code is now :

<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
var $Message = "Default Message";

function Test($Message)
{
$this->Message=$Message;
}

function Display_Message()
{
this->echo Message;
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>

But it still does not work!! Can anyone help? Thanks again for your help.


In function Display_Message(),

this->echo Message;

should be

echo this->Message;

--
Jasper Bryant-Greene
Cabbage Promotions
Jul 17 '05 #9
"Jasper Bryant-Greene" <ja**@fatalnetwork.com> wrote in message
news:ff********************@news.xtra.co.nz...
siu02rk wrote:


Hi All,

My code is now :

<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
var $Message = "Default Message";

function Test($Message)
{
$this->Message=$Message;
}

function Display_Message()
{
this->echo Message;
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>

But it still does not work!! Can anyone help? Thanks again for your
help.
In function Display_Message(),

this->echo Message;

should be

echo this->Message;

--
Jasper Bryant-Greene
Cabbage Promotions


And the last line of your PHP block should be
$TestObject->Display_Message();

Capitalization matters when it comes to variable names. As you had it,
$Testobject contains nothing, but $TestObject contains the object Test.

- JP
Jul 17 '05 #10
siu02rk wrote:
<html>
<title> This is my test for creating a class </test>
<body>
<?php
class Test
{
var $Message = "Default Message";

function Test($Message)
{
$this->Message=$Message;
}

function Display_Message()
{
this->echo Message;
}
}

$TestObject = new Test("Hello World!!");
$Testobject->Display_Message();
?>
</body>
</html>
Please set your error_reporting to E_ALL and have a look at the error
messages. If you would do that and would care to read the documentation,
you could stop bothering us with this basic stuff.
this->echo Message;
This line produces the following error:

| Parse error: parse error, unexpected T_OBJECT_OPERATOR in [filename] on line 16

You've made two mistakes here. First you forgot the $ as the first
character of the variable name. Then you're trying to call a function
echo() in this class with an undefined local variable named $Message.
What you really want is:
echo $this->Message;

$Testobject->Display_Message();


This line produces the following error:

| Fatal error: Call to a member function on a non-object in [filename] on line 21

You created an instance of the class with the name $TestObject and you
try to access a variable with the name $Testobject. Do you see the
difference? Change this to:
$TestObject->Display_Message();
Matthias
Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...
0
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
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...

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.