Edit report at https://bugs.php.net/bug.php?id=54170&edit=1

 ID:                 54170
 Comment by:         MonYeaTea at ist dot mit dot edu
 Reported by:        martijn at site-to-make dot nl
 Summary:            Object instantion argument not executed when the
                     constructor is missing
 Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Debian 6.0
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

Well, the problem is that PHP does not break down here.

It's okay to optimize here, and as Rasmus named it first that there is 
an evaluation optimization with the new operator, let's start to name 
that optimization after him: Rasmus evaluation.

This Rasmus evaluation is also the case with any other non-existent 
function or method. The arguments to these aren't evaluated either.

However - and this seems to be missing here - these function/method 
calls are producing fatal errors - the missing constructor function 
doesn't.

The workaround (in your case) is to not use the new operator but to 
use ReflectionClass::newInstance():


<?php

class A {}

(new ReflectionClass('A'))->newInstance(new ThisIsANotExistingClass());


However there is a caveat: With this workaround you do not profit from 
the Rasmus evaluation any longer. The fatal error will be thrown *after* 
the arguments have been evaluated. This is because of the layer of 
indirection the ReflectionClass adds here.

Hope this helps until PHP is fixed and throws a fatal error when a 
non-existent constructor is called in the future.

Then you can profit safely from Rasmus evaluation again.


Previous Comments:
------------------------------------------------------------------------
[2011-03-05 19:47:52] info at liefcoden dot nl

Well then, let me tell my customers their applications might break down because 
of this "feature". Thanks very much.

------------------------------------------------------------------------
[2011-03-05 19:41:41] ras...@php.net

Without a constructor the arguments to the new call mean nothing and are 
optimized away. The object is still created:

class A {  }
$b = new A(new ThisIsANotExistingClass());
var_dump($b);

outputs:

object(A)#1 (0) {
}

------------------------------------------------------------------------
[2011-03-05 19:33:32] info at liefcoden dot nl

As an addition (because it is probably the same problem):

class B {
  public function __construct( ) {
    echo 'B::__construct();';
  }
}

class A { }

new A( new B( ) );

The above code prints nothing as well.

------------------------------------------------------------------------
[2011-03-05 19:21:04] martijn at site-to-make dot nl

Description:
------------
I runned in a very weird issue. When i create a new object, with another object 
instantation as argument, the 2nd object is not created if the first class has 
no constructor. It looks the arguments are simply not executed when the 
constructor is missing. When a constructor is added, it works like it should.

I tested this with multiple 5.3 versions and its confirmed as not working by 
multiple people.

Test script:
---------------
<?php

class A {}

new A(new ThisIsANotExistingClass());


Expected result:
----------------
A nice fatal error since ThisIsANotExistingClass is missing.

Actual result:
--------------
The scripts runs fine and nothing apears on the screen.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=54170&edit=1

Reply via email to