"PHP Fatal error: Uncaught Error: Class 'Locale' not found" Fix for cPanel

Sheldon P.
47-cover.jpg

Preface

I'm mostly writing this for myself as a reminder to ... myself. I just spent a half hour looking up the fix to this problem only to realize that I should have known exactly where to look.

 

What the Error Message Is Trying To Say

So "PHP Fatal error: Uncaught Error: Class 'Locale' not found" is super vague ...

First off, where is this message found? When you try and run your PHP script, you get a 500 error from the server. The first thing you should do is look at the server logs.

I'm highlighting this step because it's always the thing that new programmers forget. For some reason, the kneejerk reaction tends to be to run back to their code (I see our interns doing this all the time). OR, they simply shut down and can't think of what to do next.

It's important to know where your server outputs errors to and to make sure that, in a development environment, you have all error turned on to identify problems. Seems straight forward but I've had to explain this point to more than one server admin.

Next, what is the server trying to tell you? Well, in a nutshell, you are trying to run load a class that doesn't exist. The thing is, this is probably not really the problem the server "thinks" it is.

The "Locale" class ships with a couple of Composer libraries and is a dependency when you enable features related to international formatting. Since Composer libraries basically take care of themselves, chances are, the actual problem is that the library isn't loading properly, not so much that it's "not found".

So why isn't the "Locale" loading properly? Well, PHP relies on a number of server settings and is usually set up to be optimized for speed. The trade-off is that a server admin will try and only load what's needed in the configuration so that PHP can process code as quickly as possible with as little server (hardware) load as possible.

The result is that many admins will have "intl" turned off by default. And since Locale is dependent on intl, it doesn't load and is thus "not found".

 

 

Ok, Whatever. How Do I Actually Fix This?

So the obvious answer to "how do I fix this?" is to simply turn on the feature. In cPanel, this should be relatively straight-forward if you have access to "PHP Selector".

  1. Log into cPanel
  2. Look for the section "Software"
  3. Look for the option "Select PHP Version" and click on the icon
  4. Look for the "intl" checkbox and make sure it is selected
  5. cPanel should auto-save your selection
  6. Go back to your PHP code to see if the problem is resolved