I just installed php 8.3 with all required extensions on ubuntu 24.04 for my laravel 11 project. But when the application tries to connect to an Oracle database, there is an exception handled with this message : "Undefined constant "Yajra\Pdo\OCI_DEFAULT"". When I make "php -m", the extension "oci8" is in the list. And I don't see any error in doing "php -v". So where is the problem from?
Assistant
The error "Undefined constant 'Yajra\Pdo\OCI_DEFAULT'" occurs because the OCI8 extension's constants are not properly recognized in the Yajra PDO namespace. Here's how to resolve it:
1. Verify OCI8 Extension is Loaded in Web Server
The oci8 extension might be enabled for CLI but not for the web server (Apache/Nginx). Check via:
bash
# For Apache (if using mod_php)
sudo apache2ctl -M | grep -i oci8
# For PHP-FPM (Nginx)
php-fpm8.3 -m | grep oci8
Check phpinfo() Output
Create a info.php file in your web root:
php
<?php phpinfo(); ?>
Access it via browser and search for oci8. If missing, the extension isn't loaded.
2. Ensure OCI8 is Enabled in php.ini
Find the correct php.ini using:
bash
php --ini # For CLI
# For web server, check the Loaded Configuration File in phpinfo()
Ensure extension=oci8 exists and is uncommented in both CLI and web server php.ini.
3. Install/Update OCI8 Extension (If Missing)
On Ubuntu 24.04 with PHP 8.3:
bash
sudo apt update
sudo apt install php8.3-oci8
sudo systemctl restart apache2 # Or php-fpm, nginx, etc.
4. Check Yajra Package Code for Namespace Issue
The error indicates the Yajra PDO class references OCI_DEFAULT without the global namespace. Inspect the Yajra code (e.g., Yajra\Pdo\PDO_OCI class) for lines like:
Update Yajra Packages
Ensure you're using the latest version compatible with PHP 8.3:
bash
composer update yajra/laravel-oci8
Temporary Workaround
Add a namespace fallback in your Laravel project's bootstrap (e.g., AppServiceProvider.php):
php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if (!defined('\Yajra\Pdo\OCI_DEFAULT')) {
define('\Yajra\Pdo\OCI_DEFAULT', \OCI_DEFAULT);
}
}
}
5. Test OCI8 Connection Manually
Create a test script to isolate OCI8 functionality: