The 'final' keyword is extremely useful. Inheritance is also useful, but can be abused and becomes problematic in large applications. If you ever come across a finalized class or method that you wish to extend, write a decorator instead.

The use of final keyword is just like that occurs in JavaIn java final has three uses 1) prevent class Inheritance 2) prevent method overriding or redifination of method in subclass 3) and to declare constants But the third point seems to be missing from the PHP I guess, as i am a java developer Currently gaining competence in PHP

The behaviour of FINAL is not as serious as you may think. A little explample:<?phpclass A { final private function method(){} }

class B extends A { private function method(){}}?>

Normally you would expect some of the following will happen:- An error that final and private keyword cannot be used together- No error as the private visibility says, that a method/var/etc. is only visible within the same class

But what happens is PHP is a little curios: "Cannot override final method A::method()"

So its possible to deny method names in subclasses! Don't know if this is a good behavior, but maybe its useful for your purpose.

You can use final methods to replace class constants. The reason for this is you cannot unit test a class constant used in another class in isolation because you cannot mock a constant. Final methods allow you to have the same functionality as a constant while keeping your code loosely coupled.

Tight coupling example (bad to use constants):

<?phpinterface FooInterface{}

class Foo implements FooInterface{ const BAR = 1;

public function __construct() { }}

interface BazInterface{ public function getFooBar();}

// This class cannot be unit tested in isolation because the actual class Foo must also be loaded to get the value of Foo::BARclass Baz implements BazInterface{ private $foo;

public function __construct(FooInterface $foo) {$this->foo = $foo; }

public function getFooBar() { return Foo::BAR; }

}

$foo = new Foo();$baz = new Baz($foo);$bar = $baz->getFooBar();?>

Loose coupling example (eliminated constant usage):

<?phpinterface FooInterface{ public function bar();}

class Foo implements FooInterface{ public function __construct() { }

final public function bar() { return 1; }}

interface BazInterface{ public function getFooBar();}

// This class can be unit tested in isolation because class Foo does not need to be loaded by mocking FooInterface and calling the final bar method.class Baz implements BazInterface{ private $foo;

This is more or less true, regardless of the fact that constant (being defined at class level or not) in PHP are only scalar (int, string, etc) while in Java they may be pure object (ex: java.awat.Color.BLACK). The only possible solution of having such kind of constant is :

?>What you've created is just an object that happens to have the same methods(a duck type). But if in the client code someone makes decision based on the type of your passed decorator they'll make incorrect decision - or to be more precise not the one that you, the author of the 'decorator' wants them to make. FYI that's the correct implementation based on GoF:<?phpclass Foo{ public method doFoo() {// do something useful and return a result}}

I haven't come across any legitimate use of a finalizing class/method and I personally think that 'final' is has no much use and is just a copy'n'pasted from Java into PHP. The keyword makes code difficult to test. If you have to create a test double from a finalized class because you'll need create a derived type to shadow the methods you don't care about. If one of them is finalized you've already lost.

Please, please, please never use this keyword anywhere in your code. By using it you are saying that you somehow magically know that your class or method is perfect and will suit every possible use of the class API you're defining and that no one will ever need to do so much as add some data checking before calling parent:: on the method.

This is never true and will never be true, so let the poor programmer that needs to extend your code do so.

If you want to have "final" in your code then you don't understand OOP.

Hopefully this will be removed in some future version of the language.