Can someone test this for me to confirm my suspicions

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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Can someone test this for me to confirm my suspicions

Post 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 :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Hmmm...must be something else then...

Thanks for confirming for me :)
Post Reply