dive-be/php-crowbar

Access private methods / properties

1.3.0 2024-03-13 14:44 UTC

This package is auto-updated.

Last update: 2024-04-13 15:06:43 UTC


README

Social Card of PHP Crowbar

Access private methods / properties

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This package allows you to access methods / properties in a class with a restricted access modifier i.e. private / protected.

Installation

You can install the package via composer:

composer require dive-be/php-crowbar

Usage

Assume the following class with a private property. It offers no way to read / write its $content property.

class SealedCrate
{
    public function __construct(
        private string $content,
    ) {}
    
    private function peek(): string
    {
        return $this->content;
    }
}

$crate = new SealedCrate('Apples');

You can get the property using the Crowbar:

Crowbar::pry($crate)->content; // Apples

You can set the property:

Crowbar::pry($crate)->content; // Original: Apples

Crowbar::pry($crate)->content = 'Strawberries';

Crowbar::pry($crate)->content; // Altered: Strawberries

You can also invoke private methods:

Crowbar::pry($crate)->peek(); // Strawberries

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email oss@dive.be instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.