Skip to content

This library provides an access to protected/private, also static, methods/properties of an object/class.

License

Notifications You must be signed in to change notification settings

PiotrPress/accessor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Accessor

This library provides an access to protected/private, also static, methods/properties of an object/class.

Installation

composer require piotrpress/accessor

Usage

Example class

class Example {
    private $privateProperty = 'privateProperty';
    protected $protectedProperty = 'protectedProperty';
    static private $staticPrivateProperty = 'staticPrivateProperty';
    static protected $staticProtectedProperty = 'staticProtectedProperty';
    
    private function privateMethod( $arg1, $arg2 ) { echo $arg1 . $arg2; }
    protected function protectedMethod( $arg1, $arg2 ) { echo $arg1 . $arg2; }
    static private function staticPrivateMethod( $arg1, $arg2 ) { echo $arg1 . $arg2; }
    static protected function staticProtectedMethod( $arg1, $arg2 ) { echo $arg1 . $arg2; }
}

Including library

require __DIR__ . '/vendor/autoload.php';

use PiotrPress\Accessor;

Calling methods

$accessor = new Accessor( new Example() );

$accessor->privateMethod( 'arg1', 'arg2' );
$accessor->protectedMethod( 'arg1', 'arg2' );

Calling static methods

$accessor = new Accessor( 'Example' );

$accessor->staticPrivateMethod( 'arg1', 'arg2' );
$accessor->staticProtectedMethod( 'arg1', 'arg2' );

Getting properties

$accessor = new Accessor( new Example() );

echo $accessor->privateProperty;
echo $accessor->protectedProperty;

Getting static properties

$accessor = new Accessor( 'Example' );

echo $accessor->staticPrivateProperty;
echo $accessor->staticProtectedProperty;

Setting properties

$accessor = new Accessor( new Example() );

$accessor->privateProperty = 'newPrivateProperty';
$accessor->protectedProperty = 'newProtectedProperty';

Setting static properties

$accessor = new Accessor( 'Example' );

$accessor->staticPrivateProperty = 'newStaticPrivateProperty';
$accessor->staticProtectedProperty = 'newStaticProtectedProperty';

License

GPL3.0