You are here: Re: [PHP] Re: Strange notation to create object « PHP « IT news, forums, messages
Re: [PHP] Re: Strange notation to create object

Posted by Jason Barnett on 06/24/05 21:09

Robert Cummings wrote:
....
>
> Yeah, *grin*. And on that note, there are times when you will actually
> want $foo = &new SomeClass(); versus $foo = new SomeClass(); since
> assigning by reference will break any previous references -- something I
> forgot to mention to Matthew Weier when he challenged the relevance of
> the snippet I sent.
>
> Cheers,
> Rob.

Indeed... Matt, hope you see this one. I was quite surprised by the
results as well!

Rob, since you're explaining this to us now then I assume that the dev
team is well aware of this issue and this is intended behavior... or is
this something that will be fixed/changed so that we don't have copies
of references floating around? Because that seems very unintuitive to
me... references to references seems like a better "default" behavior
unless there's a good reason why we shouldn't change.

#!/usr/local/bin/php

<?php

class a
{
public $changes = 1;

function __construct() {
$this->changes++;
}
}

class b
{
}

$aObj = new a();
$aRef = &new a();
$bObj = new b();

$foo1 = $aObj;
$foo2 = $aObj;
$foo3 = $foo1;
$foo4 = &$foo2;
$foo5 = $aRef;
$foo6 = &$aRef;

$foo1->changes = 'I changed!';

echo "------------------\n";
print_r( $foo1 );
print_r( $foo2 );
print_r( $foo3 );
print_r( $foo4 );
print_r( $foo5 );
print_r( $foo6 );

$foo1 = $bObj;
$foo2 = $bObj;
$foo5 = $bObj;

echo "------------------\n";
print_r( $foo1 );
print_r( $foo2 );
print_r( $foo3 );
print_r( $foo4 );
print_r( $foo5 );
print_r( $foo6 );

$foo1 = &$aObj;
?>


--
NEW? | http://www.catb.org/~esr/faqs/smart-questions.html
STFA | http://marc.theaimsgroup.com/?l=php-general&w=2
STFM | http://php.net/manual/en/index.php
STFW | http://www.google.com/search?q=php

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация