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)?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
I put the class in its own file, now I have a different error - Class MyApp\SplEnum not found.
It is looking for it in the namespace. Make it \SplEnum or put use SplEnum in the top of the file under the namespace
That would probably be it however I think perhaps I do not have spl_types installed stackoverflow.com/questions/6846395/… that might be the problem? Running XAMPP on Windows. Can you recommend another way I can implement similar functionality?
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 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.
1

I think you have to import the enum class :

namespace MyApp\Enums;
use SplEnum; // specify correct path if needed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.