PHPackages                             icanboogie/accessor - 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. icanboogie/accessor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

icanboogie/accessor
===================

Implements getters/setters, read-only/write-only properties, volatile defaults, type control…

v4.2.0(3y ago)557.4k↓24.3%6BSD-3-ClausePHPPHP &gt;=7.2.5

Since Jan 31Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ICanBoogie/Accessor)[ Packagist](https://packagist.org/packages/icanboogie/accessor)[ Docs](https://icanboogie.org/)[ RSS](/packages/icanboogie-accessor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (12)Used By (6)

Accessor
========

[](#accessor)

[![Release](https://camo.githubusercontent.com/4515190bf8068cf7564600333226740293cafb4700b52fe1c402f8f4b0d0869a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6963616e626f6f6769652f6163636573736f722e737667)](https://packagist.org/packages/icanboogie/accessor)[![Code Quality](https://camo.githubusercontent.com/5b6dc1227274bd898d8c98add7071c2410496f7bc2ccc9a341004892ae25fdce/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f4943616e426f6f6769652f4163636573736f722e737667)](https://scrutinizer-ci.com/g/ICanBoogie/Accessor)[![Code Coverage](https://camo.githubusercontent.com/b9af19400031ee0c33985f6ccae4f9d8acb0a1b1c783182fed3f57389b475fcd/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f4943616e426f6f6769652f4163636573736f722e737667)](https://coveralls.io/r/ICanBoogie/Accessor)[![Downloads](https://camo.githubusercontent.com/b41092f191e2c543a47b10e1942c2341c3d3e71062ca3753cf50062778c1bb72/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6963616e626f6f6769652f6163636573736f722e737667)](https://packagist.org/packages/icanboogie/accessor)

The **icanboogie/accessor** package allows classes to implement [ICanBoogie](https://icanboogie.org)'s accessor design pattern. Using a combination of getters, setters, properties, and property visibilities, you can create read-only properties, write-only properties, virtual properties; and implement defaults, type control, guarding, and lazy loading.

#### Installation

[](#installation)

```
composer require icanboogie/inflector
```

### Preamble

[](#preamble)

Because the package is a citizen of [ICanBoogie](https://icanboogie.org)'s realm, which elected [snake case](http://en.wikipedia.org/wiki/Snake_case) a long time ago for its readability, the following examples use the same casing, but [CamelCase](http://en.wikipedia.org/wiki/CamelCase) is equally supported as we'll learn by the end of this document. Actually, because all getters and setters are formatted using the `accessor_format` trait method it is very easy to bind the formatting to one's requirements simply by overriding that method.

Getters and setters
-------------------

[](#getters-and-setters)

A getter is a method that gets the value of a specific property. A setter is a method that sets the value of a specific property. You can define getters and setters on classes using the [AccessorTrait](https://icanboogie.org/api/accessor/master/class-ICanBoogie.Accessor.AccessorTrait.html) trait, and optionally inform of its feature by implementing the [HasAccessor](https://icanboogie.org/api/accessor/master/class-ICanBoogie.Accessor.HasAccessor.html) interface.

**Something to remember**: Getters and setters are only invoked when their corresponding property is not accessible. This is most notably important to remember when using lazy loading, which creates the associated property when it is invoked.

**Another thing to remember**: You don't *need* to use getter/setter for everything and their cats, PHP is no Java, and it's okay to have public properties.

Read-only properties
--------------------

[](#read-only-properties)

Read-only properties are created by defining only a getter. A [PropertyNotWritable](https://icanboogie.org/api/common/1.2/class-ICanBoogie.PropertyNotWritable.html) exception is thrown in attempt to set a read-only property.

The following example demonstrates how a `property` read-only property can be implemented:

```
