Bug #68597 [Opn->Nab]: Wrong Strict report when overriding a Trait property

From: Date: Mon, 19 Oct 2020 09:41:40 +0000
Subject: Bug #68597 [Opn->Nab]: Wrong Strict report when overriding a Trait property
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229736@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68597&edit=1

 ID:                 68597
 Updated by:         cmb@php.net
 Reported by:        pedronaroga at gmail dot com
 Summary:            Wrong Strict report when overriding a Trait property
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Windows
 PHP Version:        5.5.19
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

> 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.


Previous Comments:
------------------------------------------------------------------------
[2014-12-12 17:47:11] pedronaroga at gmail dot com

Uhh, I'm sorry, the use MyTrait; should be in MyParentClass.

------------------------------------------------------------------------
[2014-12-12 17:43:38] 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 [...]


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



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


Thread (3 messages)

« previous php.bugs (#229736) next »