/php-crowbar

Access private methods / properties

Primary LanguagePHPMIT LicenseMIT

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.