# dustin/encapsulation

**dustin/encapsulation** is a simple but powerful utility for working with objects and arrays. The main idea is to simplify data handling, structorizing, data exchange between objects, data accomodation and data extraction from objects.

Developing your data objects with encapsulations brings a lot of advantages:

* Initialize an object with data in the constructor
* Flexible setting and getting values with field name as string
* Access multiple field values at once
* Add items to arrays
* List all fields of an object
* Convert an object into an array
* Using an object like an array
* Iterate over all fields of an object
* Calculate intersections and differences between objects
* ...

## Installation

**dustin/encapsulation** is freely available on packagist. If you do not know how to use composer read the guide at here:

{% embed url="<https://getcomposer.org/doc/00-intro.md>" %}

Run the following command within your project directory to add the package as composer dependency. The command assumes you have composer installed globally.

```
composer require dustin/encapsulation
```

Now composer should have added the newest version of **dustin/encapsulation** to your *composer.json* file like this:

```json
...

"require": {
    ...
    "dustin/encapsulation": "^2.0"
    ...
}
...
```

## Introduction

An encapsulation is an object which holds key-based data. They consist of three different types which are able to exchange data among each other or to the outside.

### ArrayEncapsulation

They hold their data in an array. You are able to deal with data in an object without declaring properties.

### PropertyEncapsulation

Inheriting from PropertyEncapsulation allows creating normal objects and their properties with the ability to harness all advantages of an encapsulation.

### Container

A container holds a list of elements and brings a lot of functions to access and modify these elements. A container does not take care of keys.

Watch the changelog here:

{% content-ref url="/spaces/UIZuqoTyK5AdroZNJb8R" %}
[Changelog](https://dustinsimon.gitbook.io/encapsulation/changelog/)
{% endcontent-ref %}

Or get started with using encapsulations:


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dustinsimon.gitbook.io/encapsulation/dustin-encapsulation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
