PHP 8.5.0 Beta 1 available for testing

Voting

: three minus one?
(Example: nine)

The Note You're Voting On

qeremy (!) gmail
10 years ago
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!"; }
}
?>

<< Back to user notes page

To Top