tixelrocks / class-constants-helper
A tiny package that makes working with PHP class constants easier
Installs: 7 540
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.5.0
Requires (Dev)
- phpunit/phpunit: ~8.0
This package is auto-updated.
Last update: 2024-11-13 16:07:12 UTC
README
A tiny package that makes working with PHP class constants easier
Have you ever noticed yourself doing stuff like this:
<?php class Artist { const ARTIST_TYPE_MUSICIAN = 0; const ARTIST_TYPE_COMEDIAN = 1; const ARTIST_TYPE_MAGICIAN = 2; }
And then somewhere later, maybe in the views:
<select> <option value="{{ Artist::ARTIST_TYPE_MUSICIAN }}">Musician</option> <option value="{{ Artist::ARTIST_TYPE_COMEDIAN }}">Comedian</option> <option value="{{ Artist::ARTIST_TYPE_MAGICIAN }}">Magician</option> </select>
Now, wouldn't you want to just iterate through them instead? They all have the same prefix after all. This package does exactly that:
$ composer require tixelrocks/class-constants-helper
Now you have a new helper function constants() loaded through composer's auto-load. We can simplify our select input now:
<select> @foreach (constants(Artist::class, 'ARTIST_TYPE') as $value) <option value="{{ $value }}">{{ $value }}</option> @endforeach </select>