{"id":16789658,"url":"https://github.com/ryangjchandler/proxy","last_synced_at":"2025-11-03T14:30:32.829Z","repository":{"id":62538604,"uuid":"403706925","full_name":"ryangjchandler/proxy","owner":"ryangjchandler","description":"Proxy method and property interactions in PHP. ⚡️","archived":true,"fork":false,"pushed_at":"2021-09-07T08:53:34.000Z","size":27,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T07:39:13.830Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryangjchandler.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null},"funding":{"github":"ryangjchandler"}},"created_at":"2021-09-06T17:26:58.000Z","updated_at":"2024-12-18T07:04:28.000Z","dependencies_parsed_at":"2022-11-02T16:15:24.039Z","dependency_job_id":null,"html_url":"https://github.com/ryangjchandler/proxy","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/ryangjchandler%2Fproxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fproxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fproxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryangjchandler%2Fproxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryangjchandler","download_url":"https://codeload.github.com/ryangjchandler/proxy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239321400,"owners_count":19619697,"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-13T08:27:57.481Z","updated_at":"2025-11-03T14:30:32.776Z","avatar_url":"https://github.com/ryangjchandler.png","language":"PHP","funding_links":["https://github.com/sponsors/ryangjchandler"],"categories":[],"sub_categories":[],"readme":"# Proxy method and property interactions in PHP.\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/proxy.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/proxy)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/proxy/run-tests?label=tests)](https://github.com/ryangjchandler/proxy/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/ryangjchandler/proxy/Check%20\u0026%20fix%20styling?label=code%20style)](https://github.com/ryangjchandler/proxy/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/proxy.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/proxy)\n\nProvides a `Proxy` class that can be used to intercept method calls, property access and updates.\n\n## Support development\n\nIf you would like to support the on going maintenance and development of this package, please consider [sponsoring me on GitHub](https://github.com/sponsors/ryangjchandler).\n\n## Installation\n\nYou can install the package via Composer:\n\n```bash\ncomposer require ryangjchandler/proxy\n```\n## Usage\n\nThis package provides a `Proxy` class that you can use to wrap any object. It allows you to intercept property access and assignments, as well as method calls.\n\nHere's an example:\n\n```php\nclass Target\n{\n    public $firstName = 'Ryan';\n\n    public $lastName = 'Chandler';\n}\n\n$proxy = new Proxy(new Target, [\n    'get' =\u003e function (Target $target, string $property) {\n        if ($property === 'fullName') {\n            return $target-\u003efirstName . ' ' . $target-\u003elastName;\n        }\n\n        return $target-\u003e{$property};\n    },\n]);\n\n$proxy-\u003efullName; // 'Ryan Chandler'\n```\n\nIf you would like to handle \"setting\" a property's value, you can add a `set` key and callback function to the handlers array.\n\n```php\n$proxy = new Proxy(new Target, [\n    'set' =\u003e function (Target $target, string $property, mixed $value) {\n        if ($property === 'fullName') {\n            $parts = explode(' ', $value);\n\n            $target-\u003efirstName = $parts[0];\n            $target-\u003elastName = $parts[1];\n        } else {\n            $target-\u003e{$property} = $value;\n        }\n    },\n]);\n```\n\nTo intercept method calls, add a `call` key to the array.\n\n```php\nclass Target\n{\n    public int $number = 10;\n}\n\n$proxy = new Proxy(new Target, [\n    'call' =\u003e function (Target $target, string $method, array $arguments) {\n        if ($method === 'toInt') {\n            return $target-\u003enumber;\n        }\n\n        return $target-\u003e{$method}(...$arguments);\n    },\n]);\n\n$proxy-\u003etoInt(); // 10\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Ryan Chandler](https://github.com/ryangjchandler)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryangjchandler%2Fproxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryangjchandler%2Fproxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryangjchandler%2Fproxy/lists"}