php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63025 Method in trait not called when called as __constructor() argument
Submitted: 2012-09-07 05:20 UTC Modified: 2012-09-09 09:02 UTC
From: hinikato at mail dot ru Assigned: laruence (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.4.6 OS: Windows 7 x64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hinikato at mail dot ru
New email:
PHP Version: OS:

 

 [2012-09-07 05:20 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-07 05:50 UTC] 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 07:04 UTC] 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 07:14 UTC] reeze dot xia at gmail dot com
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,
 [2012-09-07 08:41 UTC] laruence@php.net
construct is a fcall after ZEND_NEW opcode, if there is construct, then no fcall 
at all(thus no argument substituation).

<?php
class Foo {
}

function a() {
    die("DIE ME!");
}

$b = new Foo(a());
?>

I'd like to mark this as a DOC bug, since agrumets eval is useless in most of 
the times.
 [2012-09-07 08:41 UTC] laruence@php.net
-Package: Scripting Engine problem +Package: Documentation problem
 [2012-09-07 08:42 UTC] laruence@php.net
s #if there is construct,#if there is no constructor,#
 [2012-09-08 15:38 UTC] reeze dot xia at gmail dot com
I found a same bug reported before:
https://bugs.php.net/bug.php?id=54162

we could mark one of them as duplicated
 [2012-09-09 06:14 UTC] hinikato at mail dot ru
@reeze.xia, ok, fine ;) I agree, that the #54162 issue can be marked as duplicated, this issue contains more information.
 [2012-09-09 09:02 UTC] laruence@php.net
as felipe closed a similar one #54162. then I think this could also be closed

see: http://news.php.net/php.bugs/173260
 [2012-09-09 09:02 UTC] laruence@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: laruence
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 12:01:35 2025 UTC