PHP Laravel class 返回几个具有静态属性的 classes - C# static class in static class

PHP Laravel class returning a couple of classes with static properties - C# static class in static class

在 C# 中,我可以创建静态 class,其中包含几个静态 class,我可以将它们用作常量的命名空间,例如:

public static class ConstantTypes{

   public static class ErrorTypes{
      public static string Log = "Log";
   }
}

比在应用程序中我可以使用:

ConstantTypes.ErrorTypes.Log

如何在 Laravel 中对 PHP 执行相同的操作?

现在我创建了两个 classes:

 class LogTypeConstants
    {
        const MYCONST = 'val';
    }

use App\Common\LogTypeConstants;

class AppConstants
{
    
    static function LogTypes() {
        
        $logTypeConstants = new LogTypeConstants();
        return $logTypeConstants;
    }
}

Laravel 控制器中我可以使用:

 $logType = AppConstants::LogTypes()::MYCONST;

有更好的方法吗?

大胆声明,除非绝对必要,否则永远不要创建静态函数,这是依赖注入的噩梦。顺便说一句,我觉得对此达成共识,只是使用常量并在 class.

上访问它们
class LogTypeConstants
{
    public const MYCONST = 'val';
}

LogTypeConstants::MYCONST; // would access it

一个完美的例子是 Symfony 如何在 Response class 上实现他们的 HTTP 状态代码。在 Laravel 中,您可以像这样访问它。

use Illuminate\Http\Response;

public function store() {
    ...
    return $this->response($object, Response::HTTP_CREATED);
}