Connecting Tech Pros Worldwide Forums | Help | Site Map

What is wrong with this code?

siu02rk
Guest
 
Posts: n/a
#1: Jul 17 '05
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.

Justin Koivisto
Guest
 
Posts: n/a
#2: Jul 17 '05

re: What is wrong with this code?


siu02rk wrote:[color=blue]
> 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;[/color]

change to:
$this->Message=$Message;
[color=blue]
> }
>
> Display_Message()
> {
> echo "$Message";[/color]

change to:
echo $this->Message;
[color=blue]
> }
> }
>
> $TestObject = new Test("Hello World!!");
> $Testobject->Display_Message();
> ?>
> </body>
> </html>
>
>
> Thanks in advance for your help.[/color]


--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Justin Koivisto
Guest
 
Posts: n/a
#3: Jul 17 '05

re: What is wrong with this code?


Justin Koivisto wrote:
[color=blue]
> siu02rk wrote:
>[color=green]
>> 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)[/color][/color]

(Missed some)
change to:
function Test($Message)
[color=blue][color=green]
>> {
>> $this->$Message=$Message;[/color]
>
>
> change to:
> $this->Message=$Message;
>[color=green]
>> }
>>
>> Display_Message()[/color][/color]

change to:
function Display_Message()
[color=blue][color=green]
>> {
>> echo "$Message";[/color]
>
>
> change to:
> echo $this->Message;
>[color=green]
>> }
>> }
>>
>> $TestObject = new Test("Hello World!!");
>> $Testobject->Display_Message();
>> ?>
>> </body>
>> </html>
>>
>>
>> Thanks in advance for your help.[/color]
>
>
>[/color]


--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Alvaro G. Vicario
Guest
 
Posts: n/a
#4: Jul 17 '05

re: What is wrong with this code?


*** siu02rk escribió/wrote (2 Sep 2004 13:22:49 -0700):[color=blue]
> I am trying to get an understanding of classes within PHP. What is
> wrong with this code:[/color]

Some quick remarks...
[color=blue]
> <?php
> class Test
> {
> $Message = "Default Message";[/color]

var $Message='Default Message';
[color=blue]
>
> Test($Message)
> {
> $this->$Message=$Message;
> }[/color]

$this->Message=$Message;

[color=blue]
>
> Display_Message()
> {
> echo "$Message";
> }[/color]

echo $this->Message;
[color=blue]
> }
>
> $TestObject = new Test("Hello World!!");
> $Testobject->Display_Message();
> ?>[/color]

$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»
--
Janwillem Borleffs
Guest
 
Posts: n/a
#5: Jul 17 '05

re: What is wrong with this code?


siu02rk wrote:[color=blue]
> I am trying to get an understanding of classes within PHP. What is
> wrong with this code:
>[/color]
[...][color=blue]
> <?php
> class Test
> {
> $Message = "Default Message";[/color]

In PHP 4 this should read:

var $Message = "...";

In PHP 5:

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

e.g.:

private $Message = "...";
[color=blue]
>
> Test($Message)
> {
> $this->$Message=$Message;
> }
>[/color]

This should read:

function Test($Message) {
$this->Message=$Message;
}
[color=blue]
> Display_Message()[/color]

This should read:

function Display_Message()
[color=blue]
> Thanks in advance for your help.[/color]

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



kingofkolt
Guest
 
Posts: n/a
#6: Jul 17 '05

re: What is wrong with this code?


"siu02rk" <siu02rk@hotmail.com> wrote in message
news:823c05f1.0409021222.59c1dfdb@posting.google.c om...[color=blue]
> 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();[/color]

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


Gary L. Burnore
Guest
 
Posts: n/a
#7: Jul 17 '05

re: What is wrong with this code?


On Thu, 02 Sep 2004 22:23:29 GMT, "kingofkolt"
<jessepNOSPAM@comcast.net> wrote:
[color=blue]
>"siu02rk" <siu02rk@hotmail.com> wrote in message
>news:823c05f1.0409021222.59c1dfdb@posting.google. com...[color=green]
>> 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();[/color]
>
>change to:
>$TestObject->Display_Message(); // (capitalization error)
>[/color]
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
================================================== =========================
siu02rk
Guest
 
Posts: n/a
#8: Jul 17 '05

re: What is wrong with this code?


Gary L. Burnore <gburnore@databasix.com> wrote in message news:<ch86uc$9od$1@blackhelicopter.databasix.com>. ..[color=blue]
> On Thu, 02 Sep 2004 22:23:29 GMT, "kingofkolt"
> <jessepNOSPAM@comcast.net> wrote:
>[color=green]
> >"siu02rk" <siu02rk@hotmail.com> wrote in message
> >news:823c05f1.0409021222.59c1dfdb@posting.google. com...[color=darkred]
> >> 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();[/color]
> >
> >change to:
> >$TestObject->Display_Message(); // (capitalization error)
> >[/color]
> 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
> ================================================== =========================[/color]



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.
Jasper Bryant-Greene
Guest
 
Posts: n/a
#9: Jul 17 '05

re: What is wrong with this code?


siu02rk wrote:
[color=blue]
> Gary L. Burnore <gburnore@databasix.com> wrote in message news:<ch86uc$9od$1@blackhelicopter.databasix.com>. ..
>[color=green]
>>On Thu, 02 Sep 2004 22:23:29 GMT, "kingofkolt"
>><jessepNOSPAM@comcast.net> wrote:
>>
>>[color=darkred]
>>>"siu02rk" <siu02rk@hotmail.com> wrote in message
>>>news:823c05f1.0409021222.59c1dfdb@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)
>>>[/color]
>>
>> 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
>>================================================ ===========================[/color]
>
>
>
>
> 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.[/color]

In function Display_Message(),

this->echo Message;

should be

echo this->Message;

--
Jasper Bryant-Greene
Cabbage Promotions
kingofkolt
Guest
 
Posts: n/a
#10: Jul 17 '05

re: What is wrong with this code?


"Jasper Bryant-Greene" <jasp@fatalnetwork.com> wrote in message
news:ff6_c.21510$N77.940582@news.xtra.co.nz...[color=blue]
> siu02rk wrote:
>[color=green]
> >
> >
> >
> >
> > 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[/color][/color]
help.[color=blue]
>
> In function Display_Message(),
>
> this->echo Message;
>
> should be
>
> echo this->Message;
>
> --
> Jasper Bryant-Greene
> Cabbage Promotions[/color]

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


Matthias Esken
Guest
 
Posts: n/a
#11: Jul 17 '05

re: What is wrong with this code?


siu02rk wrote:
[color=blue]
> <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>[/color]

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.
[color=blue]
> this->echo Message;[/color]

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;

[color=blue]
> $Testobject->Display_Message();[/color]

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
Closed Thread