PHP Doku:: Final-Schlüsselwort - language.oop5.final.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchSprachreferenzKlassen und ObjekteFinal-Schlüsselwort

Ein Service von Reinhard Neidl - Webprogrammierung.

Klassen und Objekte

<<Magische Methoden

Objekte klonen>>

Final-Schlüsselwort

PHP 5 führt das final-Schlüsselwort ein, welches Kindklassen davon abhält, Methoden zu überschreiben, indem man der Definition final voranstellt. Wenn die Klasse selbst als final definiert wird, kann sie nicht erweitert werden.

Beispiel #1 Beispiel für final-Methoden

<?php
class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() aufgerufen\n";
   }

   final public function 
moreTesting() {
       echo 
"BaseClass::moreTesting() aufgerufen\n";
   }
}

class 
ChildClass extends BaseClass {
   public function 
moreTesting() {
       echo 
"ChildClass::moreTesting() aufgerufen\n";
   }
}
// Erzeugt einen fatalen Fehler: Cannot override final method BaseClass::moreTesting()
?>

Beispiel #2 Beispiel für final-Klassen

<?php
final class BaseClass {
   public function 
test() {
       echo 
"BaseClass::test() aufgerufen\n";
   }

   
// Es macht hier keinen Unterschied, ob die Methode final ist oder nicht
   
final public function moreTesting() {
       echo 
"BaseClass::moreTesting() aufgerufen\n";
   }
}

class 
ChildClass extends BaseClass {
}
// Erzeugt einen fatalen Fehler: Class ChildClass may not inherit from final class (BaseClass)
?>

Hinweis: Eigenschaften können nicht als final deklariert werden; nur Klassen und Methoden können als final deklariert werden.


8 BenutzerBeiträge:
- Beiträge aktualisieren...
Anonymous
28.09.2010 16:06
The behaviour of FINAL is not as serious as you may think. A little explample:
<?php
class 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.
Baldurien
31.08.2010 22:37
"Note for Java developers: the 'final' keyword is not used for class constants in PHP. We use the keyword 'const'."

http://php.net/manual/en/language.oop5.constants.php

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 :

<?php
class Bar {...}
class
Foo {
  public static
$FOOBAR;

  static function
__init() {
    static
$init = false;
    if (
$init) throw new Exception('Constants were already initialized');
   
self::$FOOBAR = new Bar();
   
$init = true;
  }
}
Foo::__init();
?>
That said, perhaps it is useless unless PHP automatically calls the __init() method.

However, one alternative that could be done in certain case is this :

<?php
function __autoload($className) {
  ... require
the file where the class is ...
  if (
interface_exists($className, false)) return;
  if (
class_exists($className, false)) {
   
$rc = new ReflectionClass($className);
    if (!
$rc->hasMethod('__init')) return;
   
$m = $rc->getMethod('__init');
    if (!(
$m->isStatic() && $m->isPrivate())) {
      throw new
Exception($className . ' __init() method must be private and static !');
    }
   
$m->invoke(null);
    return;
  }
  throw new
Exception('Class or interface not found ' . $className);
}
?>

This can only work when one class is defined per file, since we are assured that __autoload() will be called to load the file containing the class.

eg:

test2.php:
<?php
class B {
 public static
$X;
 private static function
__init() {
   echo
'B', "\n";
  
self::$X = array(1, 2);
 }
}
class
A {
  public static
$Y;
  private static function
__init() {
    echo
'A', "\n";
   
self::$Y = array(3, 4);
  }
}
?>
test.php:
<?php
function __autoload($n) {
  if (
$n == 'A' || $n == 'B') require 'test2.php';
  ... do
our __init() trick ...
}
var_dump(B::$X); // shows B, then array(2) (1, 2)
var_dump(A::$Y); // shows NULL.
?>
Kiam
1.08.2009 1:49
The final keyword doesn't change the visibility of a property / method, which is public by default.
jriddy at gmail dot com
17.07.2009 23:20
Note for Java developers: the 'final' keyword is not used for class constants in PHP. We use the keyword 'const'.

http://php.net/manual/en/language.oop5.constants.php
sunil dot boodram at gmail dot com
19.03.2009 6:54
Using the scope resolution operator, we can easily access constants, static variables, public functions and final functions from Final Classes

Note: even by omitting the "public" keyword from the "nightNews()" method, we can see that the function defaults to public even if prefixed with the keyword final.

<?php
final class news{
 
  const
micCheck = "test1"; // will work
 
static public $promptCheck = "test2"; //will work
 
  //will work
 
public function morningNews(){
    print
'good morning world!';
  }

 
//will work
 
final public function eveningNews(){
    print
'good evening world!';
  }

 
//will work
 
final function nightNews(){
    print
'good night world!';
  }

}

print
news::micCheck; // output: test1
print '<br/>';
print
news::$promptCheck; // output: test2
print '<br/>';
print
news::morningNews(); // output: good morning world!
print '<br/>';
print
news::eveningNews(); // output: good evening world!
print '<br/>';
print
news::nightNews(); // output: good night world!
?>
santoshjoshi2003 at yahoo dot co dot in
5.12.2008 11:45
The use of final keyword is just like that occurs in Java
In 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
slorenzo at clug dot org dot ve
31.10.2007 8:13
<?php
class parentClass {
    public function
someMethod() { }
}
class
childClass extends parentClass {
    public final function
someMethod() { } //override parent function
}

$class = new childClass;
$class->someMethod(); //call the override function in chield class
?>
penartur at yandex dot ru
22.03.2007 10:39
Note that you cannot ovverride final methods even if they are defined as private in parent class.
Thus, the following example:
<?php
class parentClass {
    final private function
someMethod() { }
}
class
childClass extends parentClass {
    private function
someMethod() { }
}
?>
dies with error "Fatal error: Cannot override final method parentClass::someMethod() in ***.php on line 7"

Such behaviour looks slight unexpected because in child class we cannot know, which private methods exists in a parent class and vice versa.

So, remember that if you defined a private final method, you cannot place method with the same name in child class.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)