Forum Moderators: coopster

Message Too Old, No Replies

static scope problems?

         

PHPycho

5:24 am on Aug 31, 2007 (gmt 0)

10+ Year Member



Hello forums!
I had the following code running under the php5 server(With E_STRICT error reporting)
Code:
[php]<?php
class Test{
private static $list;

private function __construct(){

}

public static function set_msg($msg)
{
self::$list[] = $msg;
}

public static function reset_msg()
{
unset(self::$list);
}

public static function get_msg()
{
return self::$list;
}

public static function test1(msg)
{
self::set_msg($msg);
}

public static function test2(msg)
{
self::set_msg($msg);
}

}
?>[/php]
Accessing portion:
[php]<?php
// including class goes here..
Test::test1("Message1");
Test::test2("Message2");
print_r(Test::get_msg());
Test::reset_msg(); // this gives the error : "Fatal error: Attempt to unset static property Test::$list"
?>[/php]

Why did such error occur? I didnt get any idea. Can anybody give a hint?

Thanks in advance to all of you

vincevincevince

5:35 am on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



private static $list;
$list is indeed static and so it refers to the class and not to the instantated object. If you want to edit a member of an object rather than a class then don't use the static keyword.