<?php /** * Created by PhpStorm. * User: xpcomrade * Date: 2015/7/29 * Time: 19:53 */ trait Sortable { abstract function uniqueId(); function compareById($object) { return $object->uniqueId() < $this->uniqueId() ? -1 : 1; } } class Bird { use Sortable; function uniqueId() { // TODO: Implement uniqueId() method. return __CLASS__ . ":{$this->id}"; } } $bird = new Bird(); echo 'compare result:' . $bird->compareById($bird);