PHP code example of wmsamolet / php-object-collections
1. Go to this page and download the library: Download wmsamolet/php-object-collections 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/ */
wmsamolet / php-object-collections example snippets
use Wmsamolet\PhpObjectCollections\ObjectCollection;
class TestEntity
{
/** @var int */
private $id;
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
}
// Create collection with values 1,2,3
$collection = new ObjectCollection(
TestEntity::class,
[
(new TestEntity())->setId(1),
(new TestEntity())->setId(2),
(new TestEntity())->setId(3),
]
);
// Print entities: #1,#2,#3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';
// If we try to add another collection to the collection
// WE WILL GET AN EXCEPTION!
class TestOtherEntity extends TestEntity
{
}
$collection->add((new TestOtherEntity())->setId(4));
use Wmsamolet\PhpObjectCollections\AbstractCollection;
class TestEntity
{
/** @var int */
private $id;
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
}
/**
* Add PhpDoc for IDE autocompletion when working with this collection
*
* @method TestEntity[] getList()
* @method null|TestEntity get(int $key)
* @method null|TestEntity getByOffset(int $key)
*/
class ExampleEntityCollection extends AbstractObjectCollection
{
/**
* Set collection item as TestEntity object class
*/
public function collectionObjectClassName(): string
{
return TestEntity::class;
}
}
// Add entities to collection
$collection = new ExampleEntityCollection([
(new TestEntity())->setId(1),
(new TestEntity())->setId(2),
(new TestEntity())->setId(3),
]);
// Print entities: #1,#2,#3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';
// If we try to add another collection or value with another to the collection
// WE WILL GET AN EXCEPTION!
class TestOtherEntity extends TestEntity
{
}
$collection->add((new TestOtherEntity())->setId(4));
$collection->add(5);
use Wmsamolet\PhpObjectCollections\TypedCollection;
// Create collection with values 1,2,3
$collection = new TypedCollection(TypedCollection::TYPE_INTEGER, [1, 2, 3]);
// Print values: 1,2,3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';
// If we try to add value with another type to the collection
// WE WILL GET AN EXCEPTION!
$collection->add('4');
$collection->add([5]);
use Wmsamolet\PhpObjectCollections\AbstractTypedCollection;
/**
* Add PhpDoc for IDE autocompletion when working with this collection
*
* @method int[] getList()
* @method null|int get(int $key)
* @method null|int getByOffset(int $key)
*/
class TestIdCollection extends AbstractTypedCollection
{
/**
* Set collection item value type
*/
public function collectionValueType(): string
{
return static::TYPE_INT;
}
}
// Create collection with ids #1,#2,#3 to collection
$collection = new TestIdCollection([1, 2, 3]);
// Print ids: #1,#2,#3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';
// If we try to add value with another type to the collection
// WE WILL GET AN EXCEPTION!
$collection->add('4');
$collection->add([5]);
php composer.phar
"wmsamolet/php-collections": "^1.0"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.