PHP final Classes and Methods

In this chapter you will learn:

  1. Why do we need final keywords
  2. Syntax to use final keywords

Why

To lock down a class so that it can't be inherited from. Or to lock down one or more methods inside a class so that they can't be overridden in a child class.

By doing this, you know that your class or methods within your class will always behave in exactly the same way.

Syntax

You can add the keyword final before a class or method definition to lock down that class or method.

Here's how to create a final class:


final class MyClass { 

} 

Here's how you make a final method:


class ParentClass { 
   public final function myMethod() { 
            echo "A method"; 
   } /*java2s .  c o m*/
} 

Next chapter...

What you will learn in the next chapter:

  1. What is an abstract class
  2. Syntax for abstract class
  3. Note for abstract class
  4. Example - abstract class
Home » PHP Tutorial » PHP Class Definition
Concept for Object Oriented Design
PHP Class Definition
PHP Create Object from Class
PHP Class Properties
PHP Iterating Object Properties
PHP Class Inheritance
PHP Overriding Methods
PHP final Classes and Methods
PHP Abstract Class
PHP Class Access Control Modifiers
PHP Class Constructor
PHP Class Destructors
PHP Class Magic Methods