php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68597 Wrong Strict report when overriding a Trait property
Submitted: 2014-12-12 17:43 UTC Modified: 2020-10-19 09:41 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: pedronaroga at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 5.5.19 OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pedronaroga at gmail dot com
New email:
PHP Version: OS:

 

 [2014-12-12 17:43 UTC] pedronaroga at gmail dot com
Description:
------------
Just recently I came up with the following E_STRICT report:

Strict standards: Declaration of Portal_IndexController::getUsuarioLogado() should be compatible with Sicneo\Controller\Action::getUsuarioLogado($fields = NULL).

I got very confused when I checked my code and saw that Sicneo\Controller\Action did not have a 'getUsuarioLogado' function. After a couple of minutes, I found out that Portal_IndexController was using a trait that had already defined a 'getUsuarioLogado' function, with the default `null` value to a `$fields` parameter.

The strict message should point to the trait instead of pointing out to the parent class.

Test script:
---------------
trait MyTrait {

    public function myFunction($test = null) {}

}

class MyParentClass {

}

class MyChildClass extends MyParentClass {

    use MyTrait;

    public function myFunction() { }

}

$obj = new MyChildClass;
$obj->myFunction();

Actual result:
--------------
Strict standards: Declaration of MyChildClass::myFunction() should be compatible with MyParentClass::myFunction($test = NULL) in [...]

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-12-12 17:47 UTC] pedronaroga at gmail dot com
Uhh, I'm sorry, the `use MyTrait;` should be in MyParentClass.
 [2020-10-19 09:41 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-10-19 09:41 UTC] cmb@php.net
> The strict message should point to the trait instead of pointing
> out to the parent class.

No.  Traits are more or less runtime assisted copy&paste reuse, so
the method is conceptionally defined on the class using the trait.
Consider renaming, e.g.

<?php
trait MyTrait {
    public function myFunction($test = null) {}
}

class MyParentClass {
    use MyTrait {MyTrait::myFunction as myOtherFunction;}
}

class MyChildClass extends MyParentClass {
    public function myOtherFunction() {}
}
?>

In this case reporting MyTrait::myOtherFunction() as being
incompatible would be even more confusing.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Oct 26 10:01:22 2020 UTC