Encapsulation
v2.0
v2.0
  • dustin/encapsulation
  • Getting started
    • Basics
      • Container
    • Advanced
      • Object maps
      • Immutable encapsulations
    • Objects with properties
    • Intersection calculation
Powered by GitBook
On this page
  • Use inheritance
  • or a flexible class
  1. Getting started
  2. Advanced

Object maps

ObjectMappings will hold a key-based map of objects of a given class. These objects are not limited to encapsulations. You can choose any class you want.

Use inheritance

use Dustin\Encapsulation\AbstractObjectMapping;

class ProductMapping extends AbstractObjectMapping {

    protected function getType(): string {
        return Product::class;
    }
    
}

or a flexible class

use Dustin\Encapsulation\ObjectMapping;

$productMapping = ObjectMapping::create(Product::class);

Now you can create a map of Product-objects.

$productMapping = new ProductMapping();
// or
$productMapping = ObjectMapping::create(Product::class);

$productMapping->set('My1004', new Product()); // is ok
$productMapping->set('CU1005', new Customer()); // will throw an exception
PreviousAdvancedNextImmutable encapsulations

Last updated 1 year ago