【问题标题】:Using laravel constants as default value for arguments in a function使用 laravel 常量作为函数中参数的默认值
【发布时间】:2021-09-09 12:29:01
【问题描述】:

我正在将我的旧 PHP 项目转换为基于 Laravel 的项目。 在我的旧项目中,我在每个页面中加载的配置文件中定义了常量。我会在这样的文件中定义常量。

define('USER_IMAGES_DIR', IMAGES_DIR."user_images/");
define("POST_IMAGES_DIR", IMAGES_DIR."post_images/");
define("FONTS_DIR", ASSET_DIR."fonts/");

每当我需要在函数中使用常量时,我​​都会直接这样做。

function make_avatar($character,$storepath=USER_IMAGES_DIR,$extension='png')
    {
        $image_name=time() . '.'.$extension;
        $path = $storepath. $image_name;
        $image = imagecreate(200, 200);
        $red = rand(0, 255);
        $green = rand(0, 255);
        $blue = rand(0, 255);
        imagecolorallocate($image, $red, $green, $blue);
        $textcolor = imagecolorallocate($image, 255,255,255);
        imagettftext($image, 100, 0, 55, 150, $textcolor,FONTS_DIR.'arial.ttf', $character);
        imagepng($image, $path);
        imagedestroy($image);
        return $image_name;
    }

现在,在 laravel 中,常量存储在 config 文件夹中,可以通过全局帮助程序(如 config('app_images_dir'))访问。我尝试像这样在函数的默认值中直接使用全局帮助器。

function make_avatar($character,$storepath=config('app.user_images_dir'),$extension='png')
    {
        $image_name=time() . '.'.$extension;
        $path = $storepath. $image_name;
        $image = imagecreate(200, 200);
        $red = rand(0, 255);
        $green = rand(0, 255);
        $blue = rand(0, 255);
        imagecolorallocate($image, $red, $green, $blue);
        $textcolor = imagecolorallocate($image, 255,255,255);
        imagettftext($image, 100, 0, 55, 150, $textcolor,FONTS_DIR.'arial.ttf', $character);
        imagepng($image, $path);
        imagedestroy($image);
        return $image_name;
    }

但是,现在当我尝试将常量用作函数的默认值时,它无法使用并显示错误。 我收到错误消息,上面写着Constant expression contains invalid operations 那么,我的问题是如何像在传统项目中一样使用这些常量?

【问题讨论】:

    标签: php laravel constants laravel-8


    【解决方案1】:

    我建议在constructor 中为路径设置默认值,然后在make_avatar 函数中处理null 值。

    class Avatar
    {
        private $storepath ;
    
        public function __construct()
        {
            $this->storepath = config('app.user_images_dir');
        }
    
        public function make_avatar($character, $storepath = null, $extension = 'png')
        {
            $image_name = time() . '.'.$extension;
            $path = $storepath ?? $this->storepath . $image_name;
            ...
        }
    }
    

    我们在这里所做的是使用空合并运算符 (??) 来检查 $storepath 争论是否为 null。如果$storepath 不是null,我们使用它的值,否则使用config 值。

    或者,如果您不打算在除make_avatar 之外的任何其他地方使用$storepath,则无需费心在构造函数中设置类属性,因此将$this->storepath 替换为config 访问器:

    $path = $storepath ?? config('app.user_images_dir') . $image_name;
    

    需要注意的是,你有两个可选参数,如果你想改变$extension的默认值,你仍然需要在使用make_avatar时为$storepath提供一个值:

    $instanceOfAvatar->make_avatar('foo', null, 'jpeg');
    

    除非您使用的是 PHP 8 和 named arguments

    【讨论】:

      【解决方案2】:

      充分尊重所有给定的答案。我想添加我的方法,您可能会觉得有帮助。

      config helper 本身就是一个函数。 PHP 中函数的规则是它的默认值必须是绝对值,而不是相对值或函数。换句话说,默认值必须是常量表达式,而不是(例如)变量、类成员或函数调用。因此,该函数不能包含使用 config() 的默认值,因为它是一个函数。但是,您可以做的是稍微调整您的代码。

      如果您使用的函数不在类内部,那么您可以在函数上方定义一个常量,然后在函数中使用它。

      define('USER_IMAGES_DIR', config('app.user_images_dir'));
      

      这样在函数中使用这个值。

      function make_avatar($character,$storepath=USER_IMAGES_DIR,$extension='png')
          {
              $image_name=time() . '.'.$extension;
              $path = $storepath. $image_name;
              $image = imagecreate(200, 200);
              $red = rand(0, 255);
              $green = rand(0, 255);
              $blue = rand(0, 255);
              imagecolorallocate($image, $red, $green, $blue);
              $textcolor = imagecolorallocate($image, 255,255,255);
              imagettftext($image, 100, 0, 55, 150, $textcolor,FONTS_DIR.'arial.ttf', $character);
              imagepng($image, $path);
              imagedestroy($image);
              return $image_name;
          }
      

      如果这个函数在一个类中,那么你可以创建一个常量,然后使用 self 关键字将它作为你的默认函数值

      public class MyHelper extends Helper
      {
          protected const path=config('app.user_images_dir') ;  
      
      
       public function make_avatar($character, $storepath = self::path, $extension = 'png')
          {
              $image_name=time() . '.'.$extension;
                  $path = $storepath. $image_name;
                  $image = imagecreate(200, 200);
                  $red = rand(0, 255);
                  $green = rand(0, 255);
                  $blue = rand(0, 255);
                  imagecolorallocate($image, $red, $green, $blue);
                  $textcolor = imagecolorallocate($image, 255,255,255);
                  imagettftext($image, 100, 0, 55, 150, $textcolor,FONTS_DIR.'arial.ttf', $character);
                  imagepng($image, $path);
                  imagedestroy($image);
                  return $image_name;
          }
      }
      

      你也可以通过其他方式检查函数内部的值,看看它是否被调用。

      public class MyHelper extends Helper
      {
        
        /*changed the variable order so that you can still call function without sending storepath varible*/
               public function make_avatar($character, $extension = 'png',$storepath = null )
                  {
                      $image_name=time() . '.'.$extension;
                      //check if storepath is null
                        $storepath=$storepath?$storepath:config('app.user_images_dir');
                          $path = $storepath. $image_name;
                          $image = imagecreate(200, 200);
                          $red = rand(0, 255);
                          $green = rand(0, 255);
                          $blue = rand(0, 255);
                          imagecolorallocate($image, $red, $green, $blue);
                          $textcolor = imagecolorallocate($image, 255,255,255);
                          imagettftext($image, 100, 0, 55, 150, $textcolor,FONTS_DIR.'arial.ttf', $character);
                          imagepng($image, $path);
                          imagedestroy($image);
                          return $image_name;
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 2010-11-16
        • 2019-09-11
        • 2016-07-17
        • 2012-08-01
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 2019-06-19
        相关资源
        最近更新 更多