When to use Final in PHP

0 votes

Although I am aware of what a final class is, I am curious about how and when it is actually necessary.

<?php
final class Foo extends Bar
{
   public function()
   {
     echo 'John Doe';
   }
}

If I've got it right, "final" allows it to extend "Foo.".

Can somebody tell me when and why to use the word "final"? I.e., is there any justification for not extending a class?

It would be useful to construct a class that extends "Bar" if, for instance, classes "Foo" and "Bar" are missing some features.

Jul 24, 2022 in PHP by Kithuzzz
• 38,000 points
432 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
I would advise against having a class final for general use. There may be some situations when it makes sense, such as when designing a complicated API or framework and wanting to ensure that users may only modify the functionality you want them to be able to control, restricting this option and making some base classes final.

For instance, it could be sensible to make an Integer class final to prevent users of your framework from modifying, let's say, the add(...) function in your class.
answered Jul 25, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,840 points
1,321 views
0 votes
2 answers

How to use basic authorization in PHP curl?

If you are saying Basic authentication, then ...READ MORE

answered Jan 3, 2021 in PHP by Manas
• 140 points
38,068 views
0 votes
0 answers

when to use index.php instead of index.html

I wanted to know when do I ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,040 points
861 views
0 votes
0 answers

How to efficiently use try...catch blocks in PHP

I was using try..catch blocks in my ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
695 views
0 votes
0 answers
0 votes
0 answers

Is it possible to write strictly typed PHP code?

Is it possible to write code like ...READ MORE

Jun 6, 2022 in PHP by Kichu
• 19,040 points
487 views
0 votes
0 answers

What does the variable $this mean in PHP?

Can someone please explain how the variable ...READ MORE

Jun 11, 2022 in PHP by narikkadan
• 63,600 points
672 views
0 votes
0 answers

What is the difference between public, private, and protected?

When and why should I use public, ...READ MORE

Jun 16, 2022 in PHP by narikkadan
• 63,600 points
526 views
0 votes
0 answers

Advantages and Disadvantages of Inheritance in php

Could someone please explain the main benefits ...READ MORE

Jul 28, 2022 in PHP by Kithuzzz
• 38,000 points
722 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP