1. Go to this page and download the library: Download jcrowe/type-safe-collection 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/ */
jcrowe / type-safe-collection example snippets
use JCrowe\TypeSafeCollection\TypeSafeCollection;
class MovieLibrary extends TypeSafeCollection {
// list of classes that can be added to the collection
protected $allowedClasses = [Watchable::class, Rentable::class];
}
$myLibrary = new MovieLibrary([
new WatchableMovie(),
new RentableDVD(),
new ReadableBook() // throws \InvalidArgumentProvided exception
]);
$myLibrary = new MovieLibrary();
$myLibarry->push(new RentableDVD());
$myLibrary->push(new ReadableBook()); // throws \InvalidArgumentProvided exception
class MovieLibrary extends TypeSafeCollection {
// list of classes that can be added to the collection
protected $allowedClasses = [Watchable::class, Rentable::class];
// this function will be called whenever a new
// element is being added to the collection
protected function onAddNewElement($element)
{
if (!$element->isAvailable()) {
return false; // or throw exception
}
}
}
class MovieLibrary extends TypeSafeCollection {
// list of classes that can be added to the collection
protected $allowedClasses = [Watchable::class, Rentable::class];
// if set to true no exception will be thrown when
// attempting to add an invalid value.
protected $ignoreInvalidElements = true;
}
$myLibrary = new MovieLibrary();
$myLibrary->put('my_rentable', new RentableDVD());
$myLibrary->get('my_rentable'); // RentableDVD object
$myLibrary->put('my_readable', new ReadableBook()); // no exception is thrown
$myLibrary->get('my_readable'); // null
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.