What potential pitfalls should be considered when sorting arrays of class instances in PHP?

When sorting arrays of class instances in PHP, one potential pitfall to consider is that the comparison function used for sorting may not be able to access private properties of the class instances. To solve this, you can create a custom comparison function within the class itself or use a public method to access the properties needed for sorting.

class MyClass {
    private $property;

    public function getProperty() {
        return $this->property;
    }
}

$objects = [new MyClass(), new MyClass()];

usort($objects, function($a, $b) {
    return $a->getProperty() <=> $b->getProperty();
});