Download the PHP package rootiteam/accessors without Composer

On this page you can find all versions of the php package rootiteam/accessors. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package accessors

Tests

Accessors

Current library can create automatic accessors (e.g. getters and setters) for object properties. It works by injecting a trait with magic methods for property overloading into desired class, which will act on attempts to access protected (inaccessible for outside) properties.

Features

Requirements

PHP >= 8.0

Installation

Install with composer:

Contents

Basic usage

Consider following class with manually generated accessor methods:

This has boilerplate code just to make 3 properties readable. In case there are tens of properties things could get quite tedious.

By using Accessible trait this class can be rewritten:

Besides the fact that boilerplate code for getters has been avoided, there's also direct assignment syntax available now, which wasn't even possible with initial class:


If there's lot's of properties to expose, then it's not reasonable to mark each one of them separately. Mark all properties at once in the class declaration:

Now turn off readability of $baz:

What about writing to properties? Yes, just add #[Setter] attribute:

Immutable properties

Objects which allow their contents to be changed are named as mutable. And vice versa, the ones who don't are immutable.

When talking about immutability, then it usually means combination of restricting the changes inside the original object, but providing functionality to copy/clone object with desired changes. This way original object stays intact and cloned object with changes can be used for new operations.

Consider the following example:

With #[Immutable] attribute things get simpler:

Notes:

Configuration inheritance

Inheritance works straightforward. Attributes in parent class declaration are inherited into derived class and can be overwritten (except #[Immutable] and #[Format]).

The internals of inheritance is illustrated with following example:

Case sensitivity in property names

Following rules apply when dealing with case-sensitivity in property names:

  1. When property is accessed with method syntax, where property name is part of method name, then it's treated as case-insensitive. Thus if for whatever reason you have properties which differ only in casing, then the last defined property is used:

  2. For all other situations, the property names are always treated as case-sensitive:

IDE autocompletion

Having accessors with magic methods can bring the disadvantages of losing somewhat of IDE autocompletion and make static code analyzers grope in the dark.

To inform static code parsers about availability of magic methods and properties, PHPDocs @method and @property should be specified in class'es DocBlock:

Unsetting properties

If there's ever necessity, then it's also possible to unset properties with using #[Delete]:

Notes:

Advanced usage

Mutator

With setters, it's sometimes necessary to have the assignable value passed through some intermediate function, before assigning to property. This function is called mutator and can be specified using #[Mutator] attribute:

It can validate or otherwise manipulate the value beeing assigned to property.

Mutator parameter must be string or array representing a PHP callable. When string is passed then it must have one of following syntaxes:

  1. <function>
  2. <class>::<method>
  3. $this-><method> ($this is replaced during runtime with the object instance where the accessor belongs)

It may contain special variable named %property% which is replaced with the property name it applies. This is useful when using separate mutator for each property but declaring it only once within class attributes.

The callable function/method must accept assignable value as first parameter and must return a value to be assigned to property.

Accessor endpoints

Current library can make use of any manually created accessor methods with prefixes set, get, isset, unset or with and followed by property name. Those are here referred as accessor endpoints.

For example, this allows seamless integration where current library provides direct assignment syntax on top of existing method syntax:

The 2 endpoints (getFoo/setFoo) will be called in every situation:

Notes:

Customizing format of accessor method names

Usually the names for accessor methods are just straightforward camel-case'd names like get<property>, set<property> and so on. But if necessary, it can be customized.

Customization can be achieved using #[Format(class-string<FormatContract>)] attribute with first parameter specifying class name. This class is responsible for parsing out accessor methods names.

Following example turns camel-case methods into snake-case:

Notes:

PHPDocs support

But "I always use PHPDoc tags in the front of my class to indicate IDE about magic properties. Wouldn't those comments act magically as configuration so I wouldn't have to type the same thing using Attributes?"

If you consider some caveats (below) then sure, they work. Class configured merely using PHPDoc tags:

Warning: this time AccessibleWithPHPDocs trait was injected instead of Accessible. This is to signal explicit usage with PHPDocs support.

The issue with PHPDoc tags is that depending of server's configuration, they may prove of beeing totally useless.

In case OPCache extension is enabled and configured with opcache.save_comments=0, then annotations from sourcecode are lost for ReflectionClass::getDocComment().

Generally when OPCache default settings are used, then comments will be preserved. But for performance reasons there's always possibility to turn them off. Thatswhy relying on this feature needs explicit consideration, to make sure that the developed code works in the environment where it's going to be deployed.

If youre not sure, then stick with Attributes. They always work.

API

Exposing properties

  1. Use margusk\Accessors\Accessible trait inside the class which properties you want to expose.
  2. Configure with following attributes:
    • Use #[Getter], #[Setter] and/or #[Delete] (from namespace margusk\Accessors\Attr) before the declaration of the property or whole you want to expose:
      • All them take optional bool parameter which can be set to false to deny specific accessor for the property or whole class. This is useful in situtations where override from previous setting is needed.
      • #[Getter(bool $enabled = true)]: allow or disable read access to property. Works in conjunction with allowing/denying isset on property.
      • #[Setter(bool $enabled = true)]: allow or disable to write access the property.
      • #[Delete(bool $enabled = true)]: allow or disable unset() of the property.
      • #[ToString(bool $enabled = true)]: convert class to string.
      • #[NotCloneable(bool $enabled = true)]: does not allow class to be cloned.
      • #[NotSerializable(bool $enabled = true)]: does not allow class to be serialized.
    • #[Mutator(string|array|null $callback)]:
      • the $callback parameter works almost like callable but with a tweak in string type:
      • if string type is used then it must contain regular function name or syntax $this->someMutatorMethod implies instance method.
      • use array type for specifying static class method.
      • and use null to discard any previously set mutator.
    • #[Immutable]: turns an property or whole class immutable. When used on a class, then it must be defined on top of hierarchy. Once defined, it can't be disabled later.
    • #[Format(class-string<FormatContract>)]: allows customization of accessor method names.

Notes:

Accessor methods:

Reading properties:

Updating mutable properties (supports method chaining):

Updating immutable properties (supports method chaining):

Unsetting properties (supports method chaining):

Checking if property is initialized (returns bool):

License

This library is released under the MIT License.


All versions of accessors with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
phpstan/phpdoc-parser Version ^1.13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package rootiteam/accessors contains the following files

Loading the files please wait ....