Partners & Affiliates














[PHP-DEV] Re: [PHP-QA] RE: [PHP-DEV] Re: [PHP-QA] $obj = new foo() patch From: Kirill Maximov (kir <email protected>)
Date: 11/10/00

  Hi Mike & others!

Mike Heath wrote:
>
> A simple fix would be:
> <?php
> class B {
> function bb() {
> global $a;
> echo "b";
> $a->aa();
> }
> }
> class A {
> function aa() {
> echo "A";
> }
> }
>
> function t() {
>
> global $a;
> $GLOBALS['a'] = new A;
> $a->aa();
>
> global $b;
> $GLOBALS['b'] = new B;
> $b->bb();
> }
>
> t();
> ?>
>
> If new is returning a reference of course this example doesn't work. The
> problem lies in the fact the global $a references the same data the real $a
> uses. The previously posted example tells the local instance of $a and $b
> to point to some other data and does not affect the real $a or $b.

  Hmm. I thought that

  $GLOBALS['a'] = new A;

  is absolute equivalent to

  global $a;
  $a = new A;

  I don't care in this example what is returned by operator 'new'.
  I suppose, that $a in second case should be absolutely equivalent to
  GLOBALS['a'] regardsless right part of '=' sign.

>
> There appears to be a trade-off here. Either allow people to use global as
> in the example posted by Maximov or apply the patch as is. Allowing new to
> return a reference is definitely needed. It is a bug that comprimises
> enterprise level OO design and forces sloppy OO code in PHP.

  Don't forget about thousands lines of code which use old behaveour.
  Anyway, I don't understand why Andi's patch can work in both cases.
  OK, 'new' returns reference. But $a should be === $GLOBALS['a'], IMO.
  Or I've missed something?

>
> One option might also be to use $obj = &new foo() Zeev pointed out that
> this wouldn't work for passing doing something like bar(&new foo()) which is
> a limitation I think we can live with. Dan Rodriguez <danjrod <email protected>>
> also has created a patch to do that.
>
> If the above option won't work than it appears we have to choose between
> forcing poor OO design and modifying coding practices. In my opinion, good
> design is far more important than changing coding practices.

  If we want to force 'rich' OO design, let's wait until PHP 5.0. IMO,
  a few peoply will upgrade to 4.0.4 if they'll have to rewrite their code
  (possibly, a LOT of code).
  But, I think, core team understands importance of compatibility issue. That's
  why Andi reversed the patch for now, isn't it? ;-)
  
  Best regards,
  KIR

  P.S. BTW, my example is from PHPLIB library.

>
> Don't worry Andi. This bug hasn't started to annoy me... it's been annoying
> me for 2 months and I'm hoping this can be rectified in 4.0.4. :)
>
> Thanks,
>
> Mike
>
> -----Original Message-----
> From: Kirill Maximov [mailto:kir <email protected>]
> Sent: Friday, November 10, 2000 3:44 AM
> To: Andi Gutmans
> Cc: PHP Development; php-qa <email protected>
> Subject: [PHP-DEV] Re: [PHP-QA] $obj = new foo() patch
>
> Andi Gutmans wrote:
> >
> > Guys,
> >
> > I have submitted a patch to the Zend Engine which is considered
> > experimental. It is supposed to fix the problem with the $obj = new foo()
> > statement when the constructor of foo() assigns $this by reference to
> other
> > symbol table entries. Basically, the Zend Engine will explicitly assign
> new
> > foo() by reference to $obj because this is pretty much the only thing
> which
> > makes sense in this case.
> > NOTE: If you do $obj2 = $obj1 = new foo(), "$obj2 = $obj1" will still
> > assign by value and not by reference.
> > This patch is only meant to fix this problematic bug which has about 5
> > entries in the bugs database.
> > Please check it out. If there are any problems/concerns with this bug it
> is
> > best to leave it out for 4.0.4 but it should not harm anyone's scripts.
> > The following bugs are probably solved now: 6529, 6896, 7454, 7455, 7482
> > Anyway, please grab a fresh CVS tree and check that your OOP code still
> > works :)
> > Thanks!
> > Andi
>
> This code doesn't work no more as it did before:
>
> <?php
> class B {
> function bb() {
> global $a;
> echo "b";
> $a->aa();
> }
> }
> class A {
> function aa() {
> echo "A";
> }
> }
>
> function t() {
>
> global $a;
> $a = new A;
> $a->aa();
>
> global $b;
> $b = new B;
> $b->bb();
> }
>
> t();
> ?>
>
> Output should be AbA.
> I get:
>
> Ab
> Fatal error: Call to a member function on a non-object in
> /h/home/kir/public_html/a.php on line 18
>
> --
>
> With best regards,
> Kirill Maximov
> QA team.
>
> --
> PHP Quality Assurance Mailing List <http://www.php.net/>
> To unsubscribe, e-mail: php-qa-unsubscribe <email protected>
> For additional commands, e-mail: php-qa-help <email protected>
> To contact the list administrators, e-mail: php-list-admin <email protected>

-- 

With best regards, Kirill Maximov

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>