<?php declare(strict_types = 1);
final class Foo implements \Stringable {
private function __construct(public readonly string $value) {
}
public static function fromString(string $string): self {
return new self($string);
}
/**
* {@inheritdoc}
*/
public function __toString(): string {
return $this->value;
}
}
$options = [
Foo::fromString('c'),
Foo::fromString('b'),
Foo::fromString('a'),
Foo::fromString('ccc'),
Foo::fromString('bcc'),
];
uasort($options, strnatcasecmp(...));
var_dump($options);
var_dump(strnatcasecmp(Foo::fromString('c'), Foo::fromString('b')));
- Output for 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
- array(5) {
[2]=>
object(Foo)#3 (1) {
["value"]=>
string(1) "a"
}
[1]=>
object(Foo)#2 (1) {
["value"]=>
string(1) "b"
}
[4]=>
object(Foo)#5 (1) {
["value"]=>
string(3) "bcc"
}
[0]=>
object(Foo)#1 (1) {
["value"]=>
string(1) "c"
}
[3]=>
object(Foo)#4 (1) {
["value"]=>
string(3) "ccc"
}
}
Fatal error: Uncaught TypeError: strnatcasecmp(): Argument #1 ($string1) must be of type string, Foo given in /in/GPV8l:33
Stack trace:
#0 /in/GPV8l(33): strnatcasecmp(Object(Foo), Object(Foo))
#1 {main}
thrown in /in/GPV8l on line 33
Process exited with code 255.
preferences:
46.3 ms | 407 KiB | 5 Q