1

I want to use some enum classes in my laravel 5 app. They are modeled after this PHP man page example: http://php.net/manual/en/class.splenum.php

The file app\Enums.php looks like this:

<?php
namespace MyApp\Enums;
class ItemStates extends SplEnum 
{
    const __default = self::Active;

    const Active = 1;
    const Pending = 2;
}

class ItemVisibility extends SplEnum
{
    const __default = self::Community;

    const Community = 1;
    const Personal = 2;
}

I want to use these from a controller. I put a use statement at the top of my controller:

use MyApp\Enums;

When I try to use the class like this:

if ($category['Family'] == CategoryFamily::General)

I get an error: Class 'MyApp\Http\Controllers\Quiz\CategoryFamily' not found

I have run compose dump-autoload in case that matters.

How can I use my Enum classes from inside controllers (multiple controllers)?

Francesco de Guytenaere
  • 4,443
  • 2
  • 25
  • 37
AnneMz
  • 484
  • 1
  • 6
  • 16

2 Answers2

2

Please correct me if I'm wrong, but Laravel uses a PSR-4 compliant autoloader. In the documentation of psr-4 it is mentioned:

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

You can check if it uses this standard in your composer.json file to be sure:

"psr-4": {
    "MyApp\\": "app/"
}

This means it won't be able to find your class. I suggest you put each class into a separate file with the same name as the class and put that in a MyApp\Enum namespace for example.

The other options you have are to include your app using the psr-0 standard in your composer.json, or to manually include your Enum file wherever you want it.

UPDATE

Once you've done this, you should be able to use SplEnum by putting use SplEnum; under the namespace .. in top of your file, if and only when you have SplTypes installed. If you're on Windows or do not want to install this PECL Extension, then I suggest this answer: https://stackoverflow.com/a/254543/2433843 with an elegant solution.

Community
  • 1
  • 1
Francesco de Guytenaere
  • 4,443
  • 2
  • 25
  • 37
  • I am new to laravel. I was wondering if putting each one in its own file was the answer, but it seemed it would be more convenient to keep them in a single file as they are just a few lines each. – AnneMz Jul 06 '15 at 22:44
  • I put the class in its own file, now I have a different error - Class MyApp\SplEnum not found. – AnneMz Jul 06 '15 at 23:59
  • 1
    It is looking for it in the namespace. Make it \SplEnum or put use SplEnum in the top of the file under the namespace – Francesco de Guytenaere Jul 07 '15 at 00:01
  • That would probably be it however I think perhaps I do not have spl_types installed http://stackoverflow.com/questions/6846395/is-it-possible-to-use-splenum-in-php-5-2-6 that might be the problem? Running XAMPP on Windows. Can you recommend another way I can implement similar functionality? – AnneMz Jul 07 '15 at 02:07
  • Yes you are correct, I do not think it is currently supported in Windows (even if that post is 4 years old; it's still not standard). The SplEnum class lacks features compared to the Enum implementation in other languages though. This answer http://stackoverflow.com/a/254543/2433843 has two very good alternatives. One simple, for if your needs are simple. and one more elaboratefor if you need validation. You could put the base Enum in a Helper namespace for example. Updated answer for people who don't read comments. – Francesco de Guytenaere Jul 07 '15 at 07:02
1

I think you have to import the enum class :

namespace MyApp\Enums;
use SplEnum; // specify correct path if needed
Kiran LM
  • 1,359
  • 1
  • 16
  • 29