Remove final from PHP packages with Unfinalize

Published
Oct 3, 2023
Written by
Benjamin Crozat
0
comments
2 minutes
read

PHP’s final keyword is a well-known feature that allows developers to restrict further inheritance or overriding of classes and methods.

But what if you need the freedom to extend or override a third-party class that has been marked as final? Meet Unfinalize from Steve Bauman, a tool that helps us be anarchists by bypassing this restriction safely and efficiently.

Introduction to Unfinalize

Unfinalize is a tool that utilizes PHP CS Fixer to remove the final keywords from classes and methods in the Composer packages you use.

Here are some of its features:

  • Safely removes final keywords from classes and methods.
  • Operates quickly and efficiently, with no performance impact.
  • Requires no additional dependencies; everything is compiled into a single PHAR file.

Let’s dive into how you can install and use Unfinalize in your project.

Install Unfinalize

Open your terminal and run the following command:

composer require stevebauman/unfinalize

Still with me? Great!

Configure Unfinalize

In your project’s composer.json file, add an “unfinalize” section. Here you’ll specify which packages should have the final keywords removed.

{
	"unfinalize": [
		"vendor/package"
	]
}

Still in your composer.json, add the unfinalize command to the scripts section so that it runs whenever you execute composer update.

{
	"scripts": {
		"post-update-cmd": [
			"@php vendor/bin/unfinalize run"
		]
	}
}

Finally, run composer update to apply the changes:

composer update

Additional Unfinalize options

Annotate classes and methods as @final

If you want to annotate classes and methods with @final instead of outright removing the final keyword, you can add the --mark-final option when running the command.

Update your composer.json like this:

{
    "scripts": {
        "post-update-cmd": [
            "@php vendor/bin/unfinalize run --mark-final"
        ]
    }
}

This will docblock the classes and methods with @final while still allow them to be inherited or overridden.

Simulate the changes using the dry run mode

To see what changes Unfinalize would make without actually altering any files, use the --dry option:

php vendor/bin/unfinalize run --dry

This will print out a list of files that would be modified.

About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub