Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrates a PHP bug: The "final" keyword for a method is ignored if used in a trait (https://bugs.php.net/bug.php?id=62204)
<?php
/**
* FinalKeywordIgnoredInTrait.php
*
* PHP 5.4.13 (cli) (built: Mar 15 2013 02:05:59)
* Copyright (c) 1997-2013 The PHP Group
* Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
* with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans
*
* @link https://bugs.php.net/bug.php?id=62204
*/
trait ExampleTrait
{
final function execute()
{
}
}
class Example
{
use ExampleTrait;
// Should raise an \E_FATAL with the following message:
// "Cannot override final method Example::execute() in [...] on line 26"
function execute()
{
}
}
@memoryza

This comment has been minimized.

Copy link

@memoryza memoryza commented Jun 18, 2013

trait isn't abide by class inherit constraint?
PHP should raise an \E_FATAL when the parent class defined a final method.

my english is not good ==!

Example

class ParentExample
{
    final function execute()
    {
        echo 'from parent';
    }

}

trait ExampleTrait
{
    function execute()
    {
    }
}

class Example extends ParentExample
{
    use ExampleTrait;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.