PHPackages                             zjkiza/default-access - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zjkiza/default-access

ActivePhp-package[Utility &amp; Helpers](/categories/utility)

zjkiza/default-access
=====================

To easily access and invoke private and protected properties and methods as with static.

v0.5.2(1y ago)07MITPHPPHP &gt;=8.0

Since Nov 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/zjkiza/default-access)[ Packagist](https://packagist.org/packages/zjkiza/default-access)[ RSS](/packages/zjkiza-default-access/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (11)Versions (3)Used By (0)

PHP Package: Default Accessor
=============================

[](#php-package-default-accessor)

A package for easily accessing and invoking private and protected properties and methods, as well as static ones. It also supports access to the methods and properties of the parent class.

About the Package
=================

[](#about-the-package)

This package provides a practical way to access and manage private and protected methods and properties in PHP objects through the `Zjk\Accessor\Contract\DefaultAccessInterface`.

Methods for Managing Properties and Methods
===========================================

[](#methods-for-managing-properties-and-methods)

- `callSetter(object $object, string $method, mixed $value): void`

    - Invokes a setter method on an object to assign a value.
- `callGetter(object $object, string $method): mixed`

    - Invokes a getter method on an object to retrieve a value.
- `callMethod(object $object, string $method, array $arguments = []): mixed`

    - Allows the invocation of any private or protected method, including those with multiple arguments. It also supports the naming of the argument.
    - Useful for: Methods that require complex parameters or perform combined operations like setting and retrieving values.
- `setValue(object $object, string $property, mixed $value): void`

    - Provides direct access to assign a value to a private or protected property.
- `getValue(object $object, string $property): mixed`

    - Provides direct access to retrieve the value of a private or protected property.

This tool is ideal for scenarios such as testing, debugging, or working with complex objects where modifying the visibility of methods and properties is not possible or practical.

Installation
============

[](#installation)

Add the package to your composer.json file:

```
composer require zjkiza/default-access

```

If you're using the Composer autoloader, all necessary files will be automatically included.

Working with the Package
========================

[](#working-with-the-package)

Usage Without a Framework
-------------------------

[](#usage-without-a-framework)

If you're working in a plain PHP project without a framework, you can directly use the `DefaultAccessor::create()` class. This class implements the DefaultAccessInterface and provides access to all its methods.

```
    use Zjk\Accessor\DefaultAccessor;

    $accessor = DefaultAccessor::create();

    $object = new SomeClass();
    $accessor->setValue($object, 'propertyName', 'value');
    $value = $accessor->getValue($object, 'propertyName');
```

Usage with a Framework
----------------------

[](#usage-with-a-framework)

If you're using a PHP framework, you can integrate this package through dependency injection.

1. Register the Interface and Implementation:

    - Configure the `Zjk\Accessor\Contract\DefaultAccessInterface` interface to instantiate the `Zjk\Accessor\DefaultAccessor` class.
    - This allows the `DefaultAccessInterface` to be injected into any class within the framework.
2. Direct Usage:

    - Alternatively, you can use the static `DefaultAccessor::create()` method for instantiation without additional configuration.

### Example in Symfony

[](#example-in-symfony)

- Service registration in services.yaml:

    ```
      services:
          Zjk\Accessor\Contract\DefaultAccessInterface:
              class: Zjk\Accessor\DefaultAccessor
    ```
- Usage in a code:

    ```
        public function someAction(Zjk\Accessor\Contract\DefaultAccessInterface $accessor)
        {
            $accessor->callSetter($object, 'setProperty', 'value');
        }
    ```

### Example in Laravel

[](#example-in-laravel)

- Add the binding in a service provider, such as `AppServiceProvider`

    ```
    use Zjk\Accessor\Contract\DefaultAccessInterface;
    use Zjk\Accessor\DefaultAccessor;

    public function register()
    {
        $this->app->bind(DefaultAccessInterface::class, function () {
            return DefaultAccessor::create();
        });
    }
    ```
- Usage in a code:

    ```
    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use Zjk\Accessor\Contract\DefaultAccessInterface;

    class ExampleController extends Controller
    {
        protected DefaultAccessInterface $accessor;

        public function __construct(DefaultAccessInterface $accessor)
        {
            $this->accessor = $accessor;
        }

        public function modifyObject()
        {
            $object = new SomeClass();

            // Set a private property
            $this->accessor->setValue($object, 'privateProperty', 'newValue');

            // Get the private property's value
            $value = $this->accessor->getValue($object, 'privateProperty');

            return response()->json(['value' => $value]);
        }
    }
    ```

Package Benefits
================

[](#package-benefits)

- **Flexibility**: Enables interaction with objects that have private or protected attributes/methods without modifying their visibility.
- **Testing Efficiency**: Facilitates isolating and validating the behavior of encapsulated objects in unit tests.
- **Simplicity**: Provides a clean and intuitive API for day-to-day use.

This package is an excellent tool for advanced PHP developers who need a powerful and practical way to access "hidden" parts of objects without compromising object-oriented design principles.

Example :
=========

[](#example-)

```
class Bar
{
    private ?string  $privateProperty;
    private static ?string $privateStaticProperty;
    protected ?string  $protectedProperty;
    protected static ?string $protectedStaticProperty;

    public function __construct(
        ?string  $protectedPropertyBar = null,
        ?string  $privatePropertyBar = null,
        ?string  $protectedStaticProperty = null,
        ?string  $privateStaticProperty = null,
    ) {
        $this->protectedProperty = $protectedPropertyBar;
        $this->privateProperty = $privatePropertyBar;
        self::$protectedStaticProperty = $protectedStaticProperty;
        self::$privateStaticProperty = $privateStaticProperty;
    }

     private function getPrivate(): string
    {
        return $this->privateProperty;
    }

    private function setPrivate(string $name): void
    {
        $this->privateProperty = $name;
    }

    protected function getProtected(): string
    {
        return $this->protectedProperty;
    }

    protected function setProtected(string $name): void
    {
        $this->protectedProperty = $name;
    }

    private static function getPrivateStatic(): string
    {
        return self::$privateStaticProperty;
    }

    private static function SetPrivateStatic(string $name): void
    {
        self::$privateStaticProperty = $name;
    }

    protected static function getProtectedStatic(): string
    {
        return self::$protectedStaticProperty;
    }

    protected static function SetProtectedStatic(string $name): void
    {
        self::$protectedStaticProperty = $name;
    }

    protected function getWithMultipleArguments(string $name, int $number): string
    {
        return $name.$number;
    }

    protected function setWithMultipleArguments(string $name, int $number): void
    {
        $this->protectedPropertyFoo = $name.$number;
    }
}

$bar = new Bar('property 1', 'property 2', 'property 3', 'property 4');

DefaultAccessor::create()->callSetter($bar, 'setPrivate', 'Input bar');
$protectedValue =  DefaultAccessor::create()->callGetter($bar, 'getProtected');

DefaultAccessor::create()->setValue($bar, 'protectedStaticPropertyBar', 'input static');
$protectedStaticValue = DefaultAccessor::create()->getValue($bar, 'protectedStaticProperty');

$this->defaultAccessor->callMethod($foo, 'setWithMultipleArguments', ['Foo', 11]);
$this->defaultAccessor->callMethod($foo, 'setWithMultipleArguments', [
    'number' => 11,
    'name' => 'Foo',
]);
$value = $this->defaultAccessor->callMethod($foo, 'getMultipleArguments', ['Foo', 11]);
$value = $this->defaultAccessor->callMethod($foo, 'getProtectedFuncFoo');
```

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~13 days

Total

2

Last Release

569d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5ff452349a91001f15f3a1d5ddf5a3a7382218f9222bc3fbfdd005d6c5f0473c?d=identicon)[zjkiza](/maintainers/zjkiza)

---

Top Contributors

[![zjkiza](https://avatars.githubusercontent.com/u/31966449?v=4)](https://github.com/zjkiza "zjkiza (6 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zjkiza-default-access/health.svg)

```
[![Health](https://phpackages.com/badges/zjkiza-default-access/health.svg)](https://phpackages.com/packages/zjkiza-default-access)
```

###  Alternatives

[tomatophp/filament-helpers

Helper Class Generator to manage your forms and table inside your filament app

128.1k](/packages/tomatophp-filament-helpers)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
