Page 1 of 1

static scope problems ??

Posted: Fri Aug 31, 2007 12:17 am
by PHPycho
Hello forums!!
I had the following code running under the php5 server(With E_STRICT error reporting)
Code:

Code: Select all

<?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);		
	}

}
?>
Accessing portion:

Code: Select all

<?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"
?>
Why did such error occur ? I didnt get any idea. Can anybody give a hint ?

Thanks in advance to all of you

Posted: Fri Aug 31, 2007 12:28 am
by Christopher
You can unset a static class property. Static means that it exists with the class definition -- not in an object where it could be unset.

Posted: Fri Aug 31, 2007 9:06 am
by Begby
It should be something like this

Code: Select all

class Test
{
  static $list = array() ;

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

}