{"id":16013230,"url":"https://github.com/sof3/php-ext-thisrc","last_synced_at":"2025-03-20T09:50:57.667Z","repository":{"id":95882324,"uuid":"261198550","full_name":"SOF3/php-ext-thisrc","owner":"SOF3","description":"PHP extension to get refcount of $this","archived":false,"fork":false,"pushed_at":"2020-08-16T08:16:14.000Z","size":18,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-25T10:41:19.119Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SOF3.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-04T14:10:35.000Z","updated_at":"2024-12-26T13:51:35.000Z","dependencies_parsed_at":"2023-04-12T15:52:27.205Z","dependency_job_id":null,"html_url":"https://github.com/SOF3/php-ext-thisrc","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Fphp-ext-thisrc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Fphp-ext-thisrc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Fphp-ext-thisrc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SOF3%2Fphp-ext-thisrc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SOF3","download_url":"https://codeload.github.com/SOF3/php-ext-thisrc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244591502,"owners_count":20477708,"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-08T14:40:31.358Z","updated_at":"2025-03-20T09:50:57.645Z","avatar_url":"https://github.com/SOF3.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-ext-thisrc\n[![Actions](https://github.com/SOF3/serde-iter/workflows/CI/badge.svg)](https://github.com/SOF3/serde-iter/actions?query=workflow%3ACI)\n![GitHub](https://img.shields.io/github/last-commit/SOF3/serde-iter)\n![GitHub](https://img.shields.io/github/stars/SOF3/serde-iter?style=social)\n\nPHP extension to get refcount of `$this`.\n\nThis extension only adds one new function `thisrc() : int`.\nIt returns the refcount of `$this` when called from a class.\n\n## Why require `$this` instead of arbitrary argument?\nWhen the parameter is passed to another function,\nthe refcount increases.\nThis might cause confusing behaviour.\nCalling the function without passing any references is more reliable.\n\n## When is refcount information useful?\nConsider an immutable value class with methods that return a modified version:\n```php\nclass Foo {\n\tprivate $inner;\n\n\tpublic function __construct(int $inner) {\n\t\t$this-\u003einner = $inner;\n\t}\n\n\tpublic function plus(int $value) : Foo {\n\t\treturn new Foo($this-\u003einner + $value);\n\t}\n\n\tpublic function times(int $value) : Foo {\n\t\treturn new Foo($this-\u003einner - $value);\n\t}\n}\n\n$three = new Foo(3);\n$sixteen = $three-\u003eplus(5)-\u003etimes(2);\n```\n\nThis creases an intermediate object `Foo(8)` in the call chain.\nIf used in hot code,\nthis allocation could be optimized away:\n\n```php\npublic function plus(int $value) : Foo {\n\tif(thisrc() === 1) {\n\t\t$this-\u003einner += $value;\n\t\treturn $this;\n\t} else {\n\t\treturn new Foo($this-\u003einner + $value);\n\t}\n}\n```\n\nThen only one new `Foo` object is created in the call chain that creates `$sixteen`,\navoiding the allocation for the intermediate `Foo(8)`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsof3%2Fphp-ext-thisrc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsof3%2Fphp-ext-thisrc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsof3%2Fphp-ext-thisrc/lists"}