tixelrocks/class-constants-helper

A tiny package that makes working with PHP class constants easier

0.1.1 2020-03-13 04:45 UTC

This package is auto-updated.

Last update: 2024-12-13 16:17:29 UTC


README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

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>