Page 1 of 1

Can someone test this for me to confirm my suspicions

Posted: Sun Mar 25, 2007 7:33 pm
by alex.barylski
I have some statics, which are inside a function...

Code: Select all

function& blah()
{
  static $obj = null;

  $obj = new Test()
  return $obj;
}

$ref =& blah();
I'm not getting expected results...and I think it's likely because of referencing statics in PHP4 anyways, simply doesn't work (life was simpler with pointers - although admittedly I've never done this before).

Can someone quickly see if I am correct in my assumption that PHP4 doesn't allow returning of static's by reference???

I'm wondering if because PHP references use a lookup table that maybe local variables (despite being static) do not become part of that same alias lookup??? Or something to that effect...

Can someone tell me what results you get?

Again PHP4 :) not 5 :(

Posted: Sun Mar 25, 2007 7:49 pm
by volka
Works fine for me.

Code: Select all

<?php
class Test {
	var $prop;
	
	function Test() {
		$this->prop = rand();
	}
}

function& blah() {
	static $obj = null;
	
	if ( is_null($obj) ) {
		$obj = new Test();
	}
	
	return $obj;
}

echo php_uname(), ' ', php_sapi_name(), ' ', phpversion(), "\n";

$ref =& blah(); echo $ref->prop, "\n";
$ref =& blah(); echo $ref->prop, "\n";
$ref =& blah(); echo $ref->prop, "\n";
Windows NT KERNSCHMELZE 5.1 build 2600 cgi-fcgi 4.4.4
7362
7362
7362

Posted: Sun Mar 25, 2007 7:51 pm
by alex.barylski
Hmmm...must be something else then...

Thanks for confirming for me :)