Skip to content

Thoughts on "Stop Using “extends” in PHP"

I was browsing and reading up about PHP. It has been some time since I have done a PHP based project after I stopped running the websites that I managed on Drupal, so I lost touch with the changes that had been implemented to the language. As I write this, PHP is currently at version 8.2.

One of the search results that ran across to not use abstract or extend classes in your domain. For the C# developers that are reading this, extended classes in PHP is the equivalent of creating a class that is derived from a base class.

The article in question can be found at https://blog.devgenius.io/stop-using-extends-in-php-37c9da1cce83. I have a counter statement to various parts of the article and I have listed them below.

Using final in Class Declaration

For those that are unaware, the final keyword in PHP is the same as the sealed keyword in C#. That said, I do agree with using the final keyword to protect classes that should not be extended or inherited by another class. A number of C# developers have mentioned in their videos and blog posts, that classes should be sealed by default to prevent leakage into other areas of the code base or project.

This statement made in the article I do agree with. By doing this, it prevents your code from having long chains of extensions.

"Stop being afraid of duplicated code"

This statement in the article made me cringe. Reason being is it violates the DRY (Don't Repeat Yourself) principle of programming. Having repeat functionality or redundancies in code, means that if that functionality of the code changes in one place, but not everywhere that it is used, then you will end up with bugs in your code that can be difficult to track down.

Not Following Own Advice

Several of the comments pointed out how the author of the post mentioned to not use "abstract" or "extends" in the code, yet the examples that he included in the article uses "extends" multiple times. I have to call it what it is... hypocritical. It is backwards to stay to not do something and then turn around and do just that.

Conclusion

The author mentioned that it has not found a reason to use "abstract" or "extends" methods or classes in his code, and that may be very true. Each programming project will have different requirements, and as a result, will vary in what is needed to meet the project goals.

I will say this, there are times where a standard should not be done or applied. There are times when they should. Based on the technology, framework, and design pattern, it is best to do the strategy that fits the situation instead of saying to never do something or making blanket statements about programming concepts.


Last update: 2022-12-06 Last update: Kenny Robinson, @almostengr
programming, class extension, php, c#, software development, solid principles