Skip to content

A tiny package that makes working with PHP class constants easier

License

Notifications You must be signed in to change notification settings

TixelRocks/class-constants-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

class-constants-helper

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>

About

A tiny package that makes working with PHP class constants easier

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages