PHP 5 object scope

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

PHP 5 object scope

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The output I get is:

Code: Select all

CxTest1::ctor()
Test
CxTest1::dtor()
?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://ca.php.net/manual/en/language.oop5.decon.php wrote:Note: Destructor is called during the script shutdown
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

Post 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.
Last edited by Christopher on Tue May 23, 2006 7:43 pm, edited 1 time in total.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

That's the Perl style. I was confused when I saw it too (just the reverse).
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

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

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

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

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

Post 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 :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

By declaring "my", you constrain the variable to the block. Otherwise, there's no way to differentiate.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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?
Post Reply