Rahul Borse Portfolio

1. Passing Null to count() and String Offsets

Problem: Calling count(null) triggers a fatal TypeError in PHP 8. Previous versions returned 0 silently.

Why it happens: PHP 8 enforces strict type checks on standard library parameters.

Legacy CodeIgniter Model Code:

$items = $this->db->get('orders')->result_array();
if (count($items) > 0) { ... } // Error if query returns null

Recommended Fix: Use empty array checks or null coalescing operator:

$items = $this->db->get('orders')->result_array() ?? [];
if (!empty($items)) { ... }

2. Helper Errors: number_format() & strtotime() with Null

Problem: number_format(null) or strtotime(null) throws TypeError: number_format(): Argument #1 ($num) must be of type float|int, null given.

Why it happens: Deprecated implicit null scalar conversions in built-in PHP functions.

Recommended Fix: Cast or fallback to default numeric values:

// Safe PHP 8 parameter handling
$formatted_price = number_format((float)($row['price'] ?? 0), 2);
$timestamp = strtotime($row['created_at'] ?? 'now');

3. Database Drivers & whereIn() Empty Array Errors

Problem: CodeIgniter's $this->db->where_in('id', $array) generates invalid SQL (WHERE id IN ()) when passed an empty array in PHP 8 context.

Recommended Fix: Validate array count prior to building active record queries:

if (!empty($category_ids) && is_array($category_ids)) {
    $this->db->where_in('category_id', $category_ids);
} else {
    $this->db->where('1 = 0'); // Safe empty set condition
}

4. Deprecated Dynamic Class Property Declarations

Problem: In PHP 8.2+, assigning to undeclared class properties raises Deprecated: Creation of dynamic property notices.

Recommended Fix: Declare properties explicitly on custom controllers and models:

class User_model extends CI_Model {
    // Explicitly declare properties in PHP 8
    public $table_name;
    
    public function __construct() {
        parent::__construct();
        $this->table_name = 'users';
    }
}

5. Session Driver & CSRF Cookie Handling

Ensure config/config.php session cookie settings align with modern browser security policies (such as setting $config['cookie_httponly'] = TRUE; and valid $config['sess_driver'] = 'database'; tables).

For broader language level changes, see PHP 5.6 to PHP 8 Migration Guide and PHP 8 Deprecated Functions Reference.

Rahul Borse

About Rahul Borse

Rahul Borse is a Web Developer specializing in PHP, Laravel, CodeIgniter, and legacy web application upgrades. Visit the About Rahul Borse page or explore Development Services.

Have a unique idea?

Let’s build your digital presence

Address

A45 Shraddha Society, Near SBI Bank, Dindoli, Surat, Gujarat-394210, India.

Our Office Location

A45 Shraddha Society, Near SBI Bank, Dindoli, Surat, Gujarat-394210, India.

Open in Google Maps