{"id":16877620,"url":"https://github.com/nikic/comparable","last_synced_at":"2025-03-22T07:31:56.101Z","repository":{"id":7913685,"uuid":"9297712","full_name":"nikic/comparable","owner":"nikic","description":"PHP extension implementing a magic \"Comparable\" interface","archived":false,"fork":false,"pushed_at":"2013-04-11T12:38:04.000Z","size":112,"stargazers_count":49,"open_issues_count":0,"forks_count":5,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-18T09:21:51.901Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nikic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-08T13:54:36.000Z","updated_at":"2021-11-17T16:46:45.000Z","dependencies_parsed_at":"2022-08-31T14:31:01.981Z","dependency_job_id":null,"html_url":"https://github.com/nikic/comparable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fcomparable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fcomparable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fcomparable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nikic%2Fcomparable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nikic","download_url":"https://codeload.github.com/nikic/comparable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244925178,"owners_count":20532873,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T15:44:31.071Z","updated_at":"2025-03-22T07:31:55.808Z","avatar_url":"https://github.com/nikic.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"Comparable interface for PHP\n============================\n\nNote: This is to the most part just code demonstrating the implementation of a magic interface for\na tutorial. I do not currently plan on proposing including such an interface for PHP itself.\n\nThis extension implements a magic `Comparable` interface for PHP:\n\n    interface Comparable {\n        static function compare($obj1, $obj2);\n    }\n\nWhen two objects (both implementing the interface) are comapred using `\u003c`, `\u003e` or `==` the\n`compare()` method will be invoked. One should not rely on which class the method is invoked\non. (Due to technical reasons for `$l \u003c $r` it will be called on the class of `$l`, but for\n`$l \u003e $r` it will be called on the class of `$r`.)\n\nThe `compare()` method can either return `null` to fall back to the default comparison behavior\nor one of the integer values `-1` (for \"smaller\"), `0` (for \"equal\") and `1` (for \"greater\"). If\nthe returned value is not an integer, it will be cast to one. If it is not one of -1, 0 or 1 it\nwill be normalized to them.\n\nAn example (not sure how much sense it makes):\n\n```php\n\u003c?php\n\nclass Point implements Comparable {\n    protected $x, $y, $z;\n\n    public function __construct($x, $y, $z) {\n        $this-\u003ex = $x; $this-\u003ey = $y; $this-\u003ez = $z;\n    }\n\n    public static function compare($p1, $p2) {\n        if ($p1-\u003ex == $p2-\u003ex \u0026\u0026 $p1-\u003ey == $p2-\u003ey \u0026\u0026 $p1-\u003ez == $p2-\u003ez) {\n            return 0;\n        }\n\n        if ($p1-\u003ex \u003c $p2-\u003ex \u0026\u0026 $p1-\u003ey \u003c $p2-\u003ey \u0026\u0026 $p1-\u003ez \u003c $p2-\u003ez) {\n            return -1;\n        }\n\n        if ($p1-\u003ex \u003e $p2-\u003ex \u0026\u0026 $p1-\u003ey \u003e $p2-\u003ey \u0026\u0026 $p1-\u003ez \u003e $p2-\u003ez) {\n            return 1;\n        }\n\n        return 1;\n    }\n}\n\n$p1 = new Point(1, 1, 1);\n$p2 = new Point(2, 2, 2);\n$p3 = new Point(1, 0, 2);\n\nvar_dump($p1 \u003c $p2, $p1 \u003e $p2, $p1 == $p2); // true, false, false\n\nvar_dump($p1 == $p1); // true\n\nvar_dump($p1 \u003c $p3, $p1 \u003e $p3, $p1 == $p3); // false, false, false\n```\n\nInstallation\n------------\n\nThe extension is installed as usual:\n\n    ./configure --enable-comparable\n    make\n    make install\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fcomparable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikic%2Fcomparable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikic%2Fcomparable/lists"}