Keep in mind; "final"
keyword is useless in traits when directly using them, unlike extending classes / abstract classes.
<?php
trait Foo {
final public function hello($s) { print "$s, hello!"; }
}
class Bar {
use Foo;
// Overwrite, no error
final public function hello($s) { print "hello, $s!"; }
}
abstract class Foo {
final public function hello($s) { print "$s, hello!"; }
}
class Bar extends Foo {
// Fatal error: Cannot override final method Foo::hello() in ..
final public function hello($s) { print "hello, $s!"; }
}
?>
But this way will finalize trait methods as expected;
<?php
trait FooTrait {
final public function hello($s) { print "$s, hello!"; }
}
abstract class Foo {
use FooTrait;
}
class Bar extends Foo {
// Fatal error: Cannot override final method Foo::hello() in ..
final public function hello($s) { print "hello, $s!"; }
}
?>
网友评论