PHP7.4.3中PHP5.x遗留代码的self未定义警告问题咨询
Let's walk through what's happening here and how to fix it, step by step.
First: What does self refer to in your code?
In PHP's class context, self is a keyword that refers to the current class itself (in this case, Array_sort). Your line usort($array, array(self,'sort_by')) is trying to tell usort to use the static method sort_by from the Array_sort class as the sorting callback. That part of your intent is correct—you just hit a syntax/parser quirk introduced in newer PHP versions.
Why the warning happens in PHP 7.4+
Older PHP versions (like 5.x) were more lenient: if you used self in a context where the parser didn't immediately recognize it as a class keyword (like inside the array() call for a callback), it would fall back to treating it as the string 'self' (since no constant named self existed).
PHP 7.4 tightened up this behavior: now it throws a warning to let you know it's assuming 'self' (and future PHP versions will turn this into a fatal error). The parser isn't correctly interpreting self as the class keyword in this array syntax anymore.
How to fix the warning (and write better code)
You have a few solid options to resolve this, plus an improvement to avoid a hidden bug in your original code:
1. Use modern callable syntax (recommended)
PHP 5.4+ supports a cleaner array syntax for callables, and using self::class explicitly returns the class name as a string. This is the most future-proof approach:
usort($array, [self::class, 'sort_by']);
self::class resolves to the full name of your Array_sort class, so the parser knows exactly what you're referring to.
2. Use the __CLASS__ magic constant
If you need compatibility with older PHP versions (pre-5.5, though that's unlikely), __CLASS__ also returns the current class name as a string:
usort($array, array(__CLASS__, 'sort_by'));
3. Explicitly use the class name as a string
You can hardcode the class name directly in the array:
usort($array, array('Array_sort', 'sort_by'));
This works, but it's less maintainable—if you rename the class later, you'll have to update this line too.
Bonus: Fix the hidden static property bug
Your original code uses static properties (self::$key and self::$type) to pass values to the sort_by method. Static properties are shared across all instances (and all calls) of the class, which means if sort_by_key is called multiple times concurrently (e.g., in a web server handling multiple requests), the values can get overwritten and cause incorrect sorting.
A better approach is to use a closure with the use keyword to pass the $key and $type directly, eliminating the need for static properties entirely:
class Array_sort { public static function sort_by_key($array, $key, $type = 'ASC') { usort($array, function($a, $b) use ($key, $type) { if ($a[$key] === $b[$key]) { return 0; } $comparison = ($a[$key] > $b[$key]) ? 1 : -1; return ($type === 'DESC') ? -$comparison : $comparison; }); return $array; } }
This is safer, cleaner, and avoids the original self warning altogether.
内容的提问来源于stack exchange,提问作者scanlin

