Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wilgucki/php-readonly-array
https://github.com/wilgucki/php-readonly-array
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/wilgucki/php-readonly-array
- Owner: wilgucki
- Created: 2015-11-30T11:17:41.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-02T08:22:21.000Z (about 9 years ago)
- Last Synced: 2024-10-12T11:46:04.956Z (3 months ago)
- Language: PHP
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#PHP Readonly Array
##Instalation
You have two options. Download
ReadonlyArray
class and include it in your project or use composer.
composer require wilgucki/php-readonly-array
If you use composer, don't forget to include autoloader in your project.
```php
require 'vendor/autoload.php';
```##Usage
Create new ReadonlyArray object and pass array to its constructor.
```php
$readonlyArray = new ReadonlyArray([
'a' => 1,
'b' => 2
]);
```Use object as array
```php
echo $readonlyArray['a']; // 1
var_dump(isset($readonlyArray['b'])); // true
var_dump(isset($readonlyArray['c'])); // false
```If you try to get value from non-existent offset, OutOfRangeException will be thrown.
You cannot set value to an existing key nor you cannot unset defined value. If you try to do so, an LogicException will be thrown.
```php
$readonlyArray['a'] = 3; // LogicException
unset($readonlyArray['c']); // LogicException
```ReadonlyArray is marked as final class, so you can't extend it.
You can iterate through ReadonlyArray using foreach loop.
```php
$readonlyArray = new ReadonlyArray([
'a' => 1,
'b' => 2
]);foreach ($readonlyArray as $k => $v) {
echo $k.' '.$v;
}
```If you need to count elements of ReadonlyArray, use
count function.
```php
$readonlyArray = new ReadonlyArray([
'a' => 1,
'b' => 2
]);count($readonlyArray);
```