Edit report at https://bugs.php.net/bug.php?id=63025&edit=1
ID: 63025
Comment by: reeze dot xia at gmail dot com
Reported by: hinikato at mail dot ru
Summary: Method in trait not called when called as
__constructor() argument
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: Windows 7 x64
PHP Version: 5.4.6
Block user comment: N
Private report: N
New Comment:
hi hinikato,
Yes, as previous email, if constructor was defined it will get called
so your func_get_args() test script works :)
.
if not it will not get called, so the params expression will not executed.
new operator is not the same as normal function call. so it behavior
differently,
Previous Comments:
------------------------------------------------------------------------
[2012-09-07 07:04:36] hinikato at mail dot ru
@reeze.xia, yes, this weird behavior, because we can do somethink like this:
function __construct() {
$args = func_get_args();
// do something with $args
}
It seems like a bug.
------------------------------------------------------------------------
[2012-09-07 05:50:03] reeze dot xia at gmail dot com
There is nothing to do with trait. look
http://3v4l.org/eOVRJ
class A {
/* no constructor public function __construct() {} */
}
class B {
public function __construct() {
die("DIE ME!");
}
}
$a = new A(new B());
// output nothing. it didn't die with "DIE ME!"
since class A didn't have any constructor, so the construct params will not
executed the same as:
if (false && $it->iWillNotBeExecuted()) {
//
}
I thought this is a document problem :)
------------------------------------------------------------------------
[2012-09-07 05:20:18] hinikato at mail dot ru
Description:
------------
Please see the code below.
If I try move the $this->getSomething() to var:
$tmp = $this->getSomething();
return new Bar($tmp);
it works as expected, however it does not work as shown below.
Test script:
---------------
class Foo {
use TFoo;
function test() {
$this->getFromTrait();
}
}
class Bar {
function some() {
die(__METHOD__);
}
}
trait TFoo {
function getFromTrait() {
return new Bar($this->getSomething());
}
function getSomething() {
// Must be called!!!
die(__METHOD__);
}
}
(new Foo())->test();
Expected result:
----------------
THe TFoo::getSomething() should be called.
Actual result:
--------------
The TFoo::getSomething()not called.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63025&edit=1