Page 1 of 2

PHP 5 object scope

Posted: Tue May 23, 2006 3:06 pm
by alex.barylski
It appears that PHP 5 (not sure of exact number) only supports two levels of scope:
1) Script
2) Function

Code: Select all

<?
	class CxTest1{
		function __construct()
		{
			echo __CLASS__.'::ctor()<br>';
		}

		function __destruct()
		{
			echo __CLASS__.'::dtor()<br>';
		}
	}

	if(1){
		$obj = new CxTest1();
	}

	echo 'Test<br>';
?>
Unfortunate, because I just encountered a problem which would best be solved using the intrinsic destruction of objects and relied only block scope to destroy objects...

But it appears this isn't the case...

Anyone know if this is a bug or issue they are looking into???

p.s-There is not point in me calling unset on the object to explicitly destroy the object...incase you are wondering :)

Cheers :)

Posted: Tue May 23, 2006 3:12 pm
by Chris Corbyn
The output I get is:

Code: Select all

CxTest1::ctor()
Test
CxTest1::dtor()
?

Posted: Tue May 23, 2006 6:42 pm
by alex.barylski
d11wtq wrote:The output I get is:

Code: Select all

CxTest1::ctor()
Test
CxTest1::dtor()
?
Yup...and thats the problem...

The object is constructed inside the IF *block* and therfore should destruct once we leave the IF block (or at least what I would expect, but it appears PHP doesn't support block scope)

However the object doesn't destruct until EOS, however objects do get destroyed on returning from functions...

Meh...no biggie...just an observation...it would have made for an interesting solution, but there are other ways :)

Cheers :)

Posted: Tue May 23, 2006 7:18 pm
by John Cartwright
http://ca.php.net/manual/en/language.oop5.decon.php wrote:Note: Destructor is called during the script shutdown

Posted: Tue May 23, 2006 7:38 pm
by nielsene
Well the other half of it... in any garbage collected languange destructors aren't called until the garbage collector runs, not immediately when the object is non referenceable. Thus you can't guess at scope from patterns of destructor invocation.

Posted: Tue May 23, 2006 7:41 pm
by Christopher
Hockey wrote:The object is constructed inside the IF *block* and therfore should destruct once we leave the IF block (or at least what I would expect, but it appears PHP doesn't support block scope)
Ok ... that qualifies as the absolutely craziest thing I have heard today.

Posted: Tue May 23, 2006 7:42 pm
by Ambush Commander
That's the Perl style. I was confused when I saw it too (just the reverse).

Posted: Tue May 23, 2006 7:58 pm
by nielsene
Not just perl... to my knowledge, C/C++,Java, etc all support block scope, so its not that crazy, just not a good way to test for it...

A better test would be more like

Code: Select all

if (1) {
  $foo="Created"
}
// if block scoped $foo should not be accessible as foo was created within the block
echo isset($foo) ? "Not Blocked Scoped" : "Blocked Scope";

Posted: Tue May 23, 2006 8:12 pm
by alex.barylski
nielsene wrote:Well the other half of it... in any garbage collected languange destructors aren't called until the garbage collector runs, not immediately when the object is non referenceable. Thus you can't guess at scope from patterns of destructor invocation.
Never even thought of that... :P

Posted: Tue May 23, 2006 8:13 pm
by alex.barylski
arborint wrote:
Hockey wrote:The object is constructed inside the IF *block* and therfore should destruct once we leave the IF block (or at least what I would expect, but it appears PHP doesn't support block scope)
Ok ... that qualifies as the absolutely craziest thing I have heard today.
Why's that?

Posted: Tue May 23, 2006 8:15 pm
by Christopher
I don't think block scope is crazy, regardless of its actual usefulnes. It is that you need scope operators like my or our to do that and there aren't any in PHP.

Posted: Tue May 23, 2006 8:20 pm
by alex.barylski
nielsene wrote:Not just perl... to my knowledge, C/C++,Java, etc all support block scope, so its not that crazy, just not a good way to test for it...

A better test would be more like

Code: Select all

if (1) {
  $foo="Created"
}
// if block scoped $foo should not be accessible as foo was created within the block
echo isset($foo) ? "Not Blocked Scoped" : "Blocked Scope";
Doesn't Java have garbage collection? Much like C#? If so doesn't that go against what you just told :? GC languages don't call constructors until after cleanup? Makes sense, but I can't see that being a requirement...
just not a good way to test for it...
What do you mean?

Without going into a lot of details, suffice it to say, I clearly understood the implications of relying on dtor() being called as it went out of scope...

I was not trying to test for anything, but rather constructing a META language using what I thought was intrinsic OO functionality...

Ever heard of Spirit? Thats what I was going after...sorta...but emulating a META language to represent a structured document (HTML for instance) therefore dtor() at script quittin' time won't work...

Cheers :)

Posted: Tue May 23, 2006 8:25 pm
by alex.barylski
arborint wrote:I don't think block scope is crazy, regardless of its actual usefulnes. It is that you need scope operators like my or our to do that and there aren't any in PHP.
I have never heard of scope operators like *my* or *our*

How would they play a role in what I was trying to do?

I simply needed an object to destruct like what I am accustomed too tis all :)

Posted: Tue May 23, 2006 8:29 pm
by Ambush Commander
By declaring "my", you constrain the variable to the block. Otherwise, there's no way to differentiate.

Posted: Tue May 23, 2006 8:33 pm
by alex.barylski
Ambush Commander wrote:By declaring "my", you constrain the variable to the block. Otherwise, there's no way to differentiate.
And PHP5 has a "my" modifier?