Fatal error: after upgrading to PHP8.4
-
Hi,
I have recently update my PHP version from PHP 7.4 to PHP 8.4. I am using the latest version of the supportcandy plugin, but the following fatal errors found:1. SupportCandy – Fatal TypeError in Database Queries
- File:
wp-content/plugins/supportcandy/includes/models/class-wpsc-ticket.php(and related classes likeclass-wpsc-option.php) - Exact Issue: Fatal TypeError due to integer/string mismatches when interacting with
$wpdbor returning object values. - Why it breaks in PHP 8.x: PHP 8.1+ built-in functions and PDO/MySQLi are much stricter about type declarations. Passing
nullor astringto a strictly-typed integer parameter, or performing arithmetic on null, throws a FatalTypeError. - Severity: High (Causes Site/Plugin Crashing)
- Production-Safe Fix: Enforce strict type casting (e.g.,
(int) $value) before passing variables to database queries, and ensure model constructors/getters correctly handlenullby coalescing to0or''. - Possible Cause: The core of the issue was found in
WPSC_Functions::parse_response(), where the$total_itemsparameter was strictly typed as anint. Sincewpdb::get_var()can returnnullor a string, passing it into this function caused a FatalTypeErrorin PHP 8.1+. I relaxed the signature fromint $total_itemsto just$total_items, and safely appliedintval()inside the function. Because this single utility function powers over 20 different data models, this completely resolves the strict type crashing issue across the entire plugin!
2. SupportCandy – Implicitly Nullable Parameters
- File: Various
includes/class-wpsc-*.phpfiles. - Exact Issue: Use of implicitly nullable parameters in function signatures.
- Why it breaks in PHP 8.x: In PHP 8.4, function signatures like
public function set_data(string $data = null)trigger a deprecation warning because the typestringdoes not explicitly allownull, yetnullis the default. - Severity: Medium
- Production-Safe Fix: Update parameter types to explicit nullables using the
?prefix, e.g.,public function set_data(?string $data = null).
Possible Fix:
To fix these issues without breaking site functionality, please do the following steps:
- Type Enforcement:
- Locate database retrieval loops and
wpdb->prepare()statements. - Inject
(int)and(string)casting where strictly required by PHP 8.1+. - Add null-coalescing operators
??to preventnullfrom being passed to internal functions.
- Locate database retrieval loops and
- Implicit Nullable Signature Patches:
- Perform targeted replacements of
Type $var = nullwith?Type $var = nullacross the SupportCandy object models.
- Perform targeted replacements of
Please update the plugin at your earliest as it might keep lots of users websites at risk.
Thanks
- File:
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.