Re: array() returns something weird

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> What it looks like to me is that something is causing $foo to be a
> string before the '$foo[] = "bar";' line is encountered. What do you
> get if you put a gettype($foo); just before that line?
>
> >        $foo=null;
> >        $foo[]="bar";      // <-- $foo simply becomes an array

NULL. That is the problem. I _did_ put a gettype($foo) before the actual line.

OK, here are exact four lines of my code:

	$ret=array();
	foreach(self::$_allowed as $r => $a)
		if ($a)
			$ret[]=$r;

As you can see, there is not a shred of a chance for $ret to become something 
other than empty array between initialization and the last line in the above 
snippet which causes the fatal errror. There's no __staticGet in 5.2.9, so 
self::$_allowed cannot have side effects.

Secondly, the above code starts failing after it has executed successfully 
dozens of times (and yes, the last line _does_ get executed; in fact self::
$_allowed contains configuration information that doesn't change at runtime).

Thirdly...

> > The problem is not limited to one place in code, and indeed before the
> > fatal caused by append-assignment I get several warnings like
> > "array_diff_key(): Argument #1 is not an array", where the offending
> > argument receives a result of array().
>
> This would appear to support my suspicion, but try inserting the
> gettype($foo) (or better, var_export($foo);) just before one of the
> lines which triggers the error, and post the results.

No, I don't think it supports your suspicion. Conversely, it indicates that 
once array() returns a strangelet, it starts returning strangelets all over 
the place. Initially it only triggers warnings but eventually one of the 
returned strangelets is used in a way that triggers a fatal error.

As per your request:

	//at the beginning of the script:

	$GLOBALS['offending_line_execution_count']=0;

	// /srv/home/[munged]/public_html/scripts/common.php line 161 and on
	// instrumented as per your request:

	public static function GetAllowed() {
	
		if (debug_mode()) echo ++$GLOBALS['offending_line_execution_count']."<br/>";
		$ret=array();
		if (debug_mode()) echo var_export($ret)."<br/>";
		foreach(self::$_allowed as $r => $a)
			if ($a)
				$ret[]=$r;

		if (self::$_allowEmpty) $ret[]="";
		return $ret;
	}

Output tail:
-----------------------------------------------
28
array ( )
29
array ( )
30
array ( )
31
array ( )
32
array ( )

Warning: array_diff_key() [function.array-diff-key]: Argument #1 is not an array 
in /srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 350

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 351

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in 
/srv/home/u80959ue/public_html/v3/scripts/SimpliciText.php on line 357

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28

Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28
33
NULL

Fatal error: [] operator not supported for strings in 
/srv/home/u80959ue/public_html/scripts/common.php on line 168
--------------------------------------------------

The warnings come from other uses of array().

But wait! There is this invocation of debug_mode() between initialization of 
$ret var_export. Let's factor it out to be safe:

		$debugmode=debug_mode();
		if ($debugmode) echo ++$GLOBALS['offending_line_execution_count']."<br/>";
		$ret=array();
		if ($debugmode) echo var_export($ret)."<br/>";

And now the output ends with:

------------------------------------
Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28
33

Fatal error: [] operator not supported for strings in 
/srv/home/u80959ue/public_html/scripts/common.php on line 169
------------------------------------

No NULL after 33? What the heck is going on? Does array() now return something 
that var_exports to an empty string, or does it destroy local variables? Let's 
see:

		if ($debugmode) echo var_export($ret)."<br/>"; else echo "WTF?!?!?<br/>";

And the output:
------------------------------------
Warning: Invalid argument supplied for foreach() in 
/srv/home/u80959ue/public_html/scripts/common.php on line 28
33
WTF?!?!?

Fatal error: [] operator not supported for strings in 
/srv/home/u80959ue/public_html/scripts/common.php on line 169
---------------------------------------

Indeed, the use of array(), once it starts misbehaving, wreaks havoc in the 
local scope (possibly including the variable to which its result is assigned).

> Can you post the code in a .zip file or online somewhere?

Unfortunately not.

Szczepan Holyszewski

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux