PHP code example of zjkiza / default-access

1. Go to this page and download the library: Download zjkiza/default-access library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zjkiza / default-access example snippets


    use Zjk\Accessor\DefaultAccessor;
    
    $accessor = DefaultAccessor::create();
    
    $object = new SomeClass();
    $accessor->setValue($object, 'propertyName', 'value');
    $value = $accessor->getValue($object, 'propertyName');


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

    use Zjk\Accessor\Contract\DefaultAccessInterface;
    use Zjk\Accessor\DefaultAccessor;
    
    public function register()
    {
        $this->app->bind(DefaultAccessInterface::class, function () {
            return DefaultAccessor::create();
        });
    }
    
    

    
    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]);
        }
    }
    

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');