static scope problems ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

static scope problems ??

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

It should be something like this

Code: Select all

class Test
{
  static $list = array() ;

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

}
Post Reply