PHP构造函数弃用报错:POMO_Reader构造函数问题求助
Hey there, let's work through this deprecation error together. The issue here is super common when upgrading PHP versions—older code that uses class-name-as-constructor syntax gets flagged because PHP 7.0+ deprecated that behavior, and it'll be removed entirely in future versions.
First, let's clear up the mistake you made earlier: renaming the constructor to class() isn't the right move. PHP's standard constructor method (for PHP 5.0+) is __construct() (note the two underscores at the start). That's the key fix here.
Here's the corrected code for your POMO_Reader class:
if ( !class_exists( 'POMO_Reader' ) ): class POMO_Reader { var $endian = 'little'; var $_post = ''; var $_pos = 0; var $is_overloaded = false; function __construct() { $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr'); $this->_pos = 0; } } endif;
Let's break down the steps to make sure this sticks:
- Fix the constructor name: Replace
function POMO_Reader()withfunction __construct(). This is the modern, PHP-standard way to define a constructor, and it'll work across all supported PHP versions (5.3+). - Explicitly declare class variables: I added explicit declarations for
$_posandis_overloadedat the top of the class. While PHP lets you create variables on the fly, declaring them upfront makes the code clearer and avoids any odd edge cases with variable scope. - Clear caches: If you still see the error after modifying the file, chances are your server is caching the old code. Clear any OPcache, Joomla page cache, or server-level caches you have enabled—sometimes you even need to restart your PHP service to get the changes to take effect.
- Double-check your PHP version: If switching PHP versions didn't help, make sure you're on a version that supports
__construct()(PHP 5.3 or newer). PHP 4 is long end-of-life, so you shouldn't be using it anyway, but it's worth confirming. - Check related files: The POMO framework has other classes (like
POMO_Writeror subclasses ofPOMO_Reader) that might have the same deprecated constructor syntax. If you still see errors after fixing this class, hunt down those other files and apply the same__construct()fix.
That should resolve the deprecation error for you. Let me know if you run into any snags!
内容的提问来源于stack exchange,提问作者Gundo Tshimauswu

