471,852 Members | 955 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

PHP5 and static functions/vars

Hi,
I need to add a string to a class var, but it doesn´t work though I read it
is a new feature in PHP5.
This is my code:

class Huh{
static $message;

public static function addMessage($message){
Huh::message += $message."<BR>\n";
}
}

What´s wrong?

Greets, Carsten
Jul 16 '05 #1
4 13263
Carsten B. wrote on Tuesday 29 July 2003 00:41:
Hi,
I need to add a string to a class var, but it doesn´t work though I read
it is a new feature in PHP5.
This is my code:

class Huh{
static $message;

public static function addMessage($message){
Huh::message += $message."<BR>\n";
}
}

What´s wrong?


I am not sure what you are trying to achieve with this. Are you trying to
modify a class definition?

You can't make non-instance calls like that.

I am guessing what you want to do is use object's method to change/update
the property; if so, you need to create an instance of the object. And, in
that case, don't define those methods and properties as static.

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 16 '05 #2
Tuesday 29 of July 2003 11:13, Zurab Davitiani wrote in comp.lang.php:

the property; if so, you need to create an instance of the object. And, in
that case, don't define those methods and properties as static.


You don't need to create an instance of an object ... the following works
for me (tested right now ;):

<?php

class Test {

static $testingval;

public static function setVal($val){

test::$testingval += $val;
echo "val: " . test::$testingval . "<BR>";

}

}

test::setVal(1);
test::setVal(4);
test::setVal(8);

?>

with results:

val: 1
val: 5
val: 13

on `official` PHP5b1

regards!

-robert-

--
Robert Jirik
[mailto:robert(at)aristoteles(dot)xhaven(dot)net]
public PGP key: http://xhaven.net/robert/pgp_key.asc
-
"In the first place, God made idiots. That was for practice.
Then he made school boards"
-- Mark Twain

Jul 16 '05 #3


Carsten B. wrote:
Hi,
I need to add a string to a class var, but it doesn´t work though I read it
is a new feature in PHP5.
This is my code:

class Huh{
static $message;

public static function addMessage($message){
Huh::message += $message."<BR>\n";
}
}

What´s wrong?

Greets, Carsten


Simple syntax problem:

Huh::$message += $message;

Jul 16 '05 #4
"Carsten B." <sp*****@web.de> schrieb:
Huh::message += $message."<BR>\n";


Shouldn't you use a "." as a string concatenation operator?

Matthias
Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by porneL | last post: by
2 posts views Thread by [Mystic] | last post: by
12 posts views Thread by Daedalus.OS | last post: by
5 posts views Thread by voronwae | last post: by
10 posts views Thread by John A Grandy | last post: by
5 posts views Thread by The Big One | last post: by

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.