PDA

Click to See Complete Forum and Search --> : PHP 4: static


nebulom
May 21st, 2007, 02:54 AM
I don't know if PHP 4 supports static but I need help on achieving this or workaround on having something like
<?php
class test {
static $a;

function b() {
if (!isset(test::$a)) {
test::$a = "hello world";
}
return test::$a;
}
}

echo test::b();

Thanks.

visualAd
May 21st, 2007, 04:41 AM
Static is supported in PHP4. Access static variables using the scope resolution operator:

test::$a

nebulom
May 21st, 2007, 08:37 PM
I've got PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in C:\Program Files\Apache Group\Apache2\htdocs\tests\_debug_tmp.php on line 6 with <?php
class test {
var $a;

function b() {
if (!isset(test::$a)) {
test::$a = "hello world";
}
return test::$a;
}
}

echo test::b();
?>:(

penagate
May 21st, 2007, 10:03 PM
I haven't quite worked out why you get a parse error but you replaced 'static' with 'var' which means you can no longer access $a statically.

nebulom
May 21st, 2007, 10:18 PM
static $a; has PHP Parse error: syntax error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in C:\Program Files\Apache Group\Apache2\htdocs\tests\_debug_tmp.php on line 3 :( I think there's no reserved keyword (for modifier) static for PHP 4. This is PHP 4 btw, pen. Thanks.

penagate
May 21st, 2007, 10:31 PM
Sorry, I don't have PHP 4 handy and so posted under the assumption that visualAd was correct. I'll remember not to do that in future. :D

There is a solution in the manual for simulating static fields in PHP 4.
http://au3.php.net/manual/en/keyword.paamayim-nekudotayim.php#58724

Hope that helps.

nebulom
May 21st, 2007, 10:37 PM
Thanks, will look at it.

visualAd
May 22nd, 2007, 12:50 AM
I seem to remember using static variables in PHP. :confused:

visualAd
May 22nd, 2007, 12:51 AM
But only in functions .... Sorry :blush: