6 kyu

Object-Oriented PHP #7 - The "Final" Keyword

Description:

Object-Oriented PHP #7 - The "Final" Keyword

About this Kata Series

In this series, we will start off by learning the very fundamentals of object-oriented programming (commonly referred to as "OOP") and classes in PHP. We will then proceed by learning about the 3 different visibilities of class properties and methods and when to use them. After that, classical inheritance will be taught along with its perks and potential drawbacks. Finally, in the final Kata(s) in this series, more advanced OOP concepts may be taught such as interfaces and even abstract classes. Apart from the main course content, every 3 Kata or so into this Series, there will be an exercise Kata to practice on previously learned knowledge.

Lesson

Don't worry, this is not the final Kata of this series ;)

Recap

In Kata #5 of this Series, we learned that a child class can inherit from a parent class via the extends keyword. We also learned that although a child class automatically inherits most properties and methods of the parent class (except for private properties and methods), it is possible to override properties and methods inherited from the parent class by simply redeclaring and redefining them.

The "Final" Keyword

Usually, all is fine and well when the properties and methods of a (parent) class can be overriden by a child class at will. If the child class wishes to keep the parent's version of a property or method, then simply no extra code is written. If, however, the child class wishes to redefine or improve upon the parent's version of a method, then it can simply redeclare and redefine it.

However, there may sometimes be situations where the parent's version of a method is so important that you want to ensure that it is never overriden by an inheriting child class (although to be honest, I personally have never found myself in this particular situation). It is so important such that you want a Fatal Error (which is unrecoverable in PHP) to be thrown if you ever accidentally redeclare or redefine that particular method in any child class that may inherit from that class containing that all-important method.

Enter the final keyword! The final keyword can be used on a class method to ensure that it cannot be overriden by any child class inheriting from that class. In terms of syntax, the final keyword is usually placed before the visibility declaration of a method, like such:

class Foo {
  final public function bar(/* arguments here */) {
    // code here
  }
}

Please note that only class methods (and classes themselves actually, but we will not cover that in this lesson) can be declared final - class properties cannot be declared final (use class constants instead).

Task

Note: The lesson provided in this Kata is designed to teach you most, if not all, of the key concepts required to complete the Task in this Kata. However, if in doubt, you are strongly encouraged to conduct your own research.

  1. Copy and paste a working solution from Kata #4 of this Series
  2. Since all humans greet extraterrestrials in the exact same way, declare the greet_extraterrestrials static method as final.
  3. Since all people also describe their jobs in the same way, make the describe_job method final too.
  4. Declare and define a new class, ComputerProgrammer, which "is a" Person ;)
  5. In the ComputerProgrammer class, override the introduce method such that it returns a string of the format "Hello, my name is NAME_HERE and I am a OCCUPATION_HERE"

Kata in this Series

This series is now complete with a total of 10 Kata, where the first 7 Kata cover the fundamentals of OOP in PHP and the last 3 Kata cover more advanced (and perhaps less-known) OOP topics in PHP.

  1. Object-Oriented PHP #1 - Classes, Public Properties and Methods
  2. Object-Oriented PHP #2 - Class Constructors and $this
  3. Object-Oriented PHP #3 - Class Constants and Static Methods
  4. Object-Oriented PHP #4 - People, people, people (Practice)
  5. Object-Oriented PHP #5 - Classical Inheritance
  6. Object-Oriented PHP #6 - Visibility
  7. Object-Oriented PHP #7 - The "Final" Keyword
  8. Object-Oriented PHP #8 - Interfaces [Advanced]
  9. Object-Oriented PHP #9 - Abstract Classes [Advanced]
  10. Object-Oriented PHP #10 - Objects on the Fly [Advanced]

You May Also Like

Object-oriented Programming
Fundamentals
Tutorials

Stats:

CreatedAug 31, 2016
PublishedAug 31, 2016
Warriors Trained959
Total Skips48
Total Code Submissions2907
Total Times Completed784
PHP Completions784
Total Stars12
% of votes with a positive feedback rating92% of 139
Total "Very Satisfied" Votes120
Total "Somewhat Satisfied" Votes16
Total "Not Satisfied" Votes3
Total Rank Assessments10
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • donaldsebleung Avatar
  • St3f4n Avatar
Ad