PHP code example of lukaszmakuch / class-based-registry

1. Go to this page and download the library: Download lukaszmakuch/class-based-registry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lukaszmakuch / class-based-registry example snippets


class Animal {}
class Elephant extends Animal {}
class Plant {}

$r = new \lukaszmakuch\ClassBasedRegistry\ClassBasedRegistry();

$r->associateValueWithClasses(
    42,
    [Animal::class]
);

$r->fetchValueByObjects([new Animal()]); //42

$r->fetchValueByObjects([new Elephant()]); //42

$r->associateValueWithClasses(
    1970,
    [Animal::class, Plant::class]
);

$r->fetchValueByObjects([new Plant(), new Elephant()]); //1970

try {
    $r->fetchValueByObjects([new Plant()]);
} catch (\lukaszmakuch\ClassBasedRegistry\Exception\ValueNotFound $e) {
  //it was not possible to fetch any value by a plant
}