Created
May 21, 2012 19:45
-
-
Save everzet/2764223 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait one { | |
public $prop; | |
protected function someMethod() | |
{ | |
// do something hidden from the | |
// outside world | |
} | |
} | |
class two { | |
use one; | |
public $prop; // "E_STRICT: This might be incompatible..." | |
public function someMethod() // But this is completely OK | |
{ | |
// do something publicly | |
} | |
} | |
// WHY ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(0); | |
trait one | |
{ | |
/** prop1 */ | |
protected $prop = 0; | |
/** method1 */ | |
public function method() {} | |
} | |
class two | |
{ | |
use one; | |
/** prop2 */ | |
protected $prop = 0; | |
/** method2 */ | |
public function method() {} | |
} | |
$two = new two(); | |
$refl = new \ReflectionObject($two); | |
echo $refl->getProperty('prop')->getDocComment() . "\n"; | |
echo $refl->getMethod('method')->getDocComment() . "\n"; | |
# | |
# Will output: | |
# | |
# /** prop1 */ | |
# /** method2 */ | |
# | |
// WHY ? |
@marijn again, i'm not talking about overriding default values or access level, i'm talking about inconsistency like that:
<?php
error_reporting(0);
trait one
{
/** prop1 */
protected $prop = 0;
/** method1 */
public function method() {}
}
class two
{
use one;
/** prop2 */
protected $prop = 0;
/** method2 */
public function method() {}
}
$two = new two();
$refl = new \ReflectionObject($two);
echo $refl->getProperty('prop')->getDocComment() . "\n";
echo $refl->getMethod('method')->getDocComment() . "\n";
will output:
/** prop1 */
/** method2 */
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, thanks for clearing that up @everzet. However, I still think that it doesn't make any sense to "change" the value of property (which might not have been anticipated by the trait author) or overwrite a docblock from the class that is using the trait. What would be the use case for changing a properties value?
I think that problem is prevented all together when properties are initialized in the constructor, opposed to in the property declaration (though I'm fuzzy at the moment how that would work in a trait).When it comes to method "changing" in the class that uses a trait, that is just plain wrong. This might cause completely erratic behavior because the method might not be functioning anymore as intended. This would be especially harmfull when that method is used by another method defined in the trait.It's too bad that the error handling is so inconsistent though.