使用 laravel 常量作为函数参数的默认值
Using laravel constants as default value for arguments in a function
我正在将我的旧 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
所以,我的问题是如何像在传统项目中那样使用这些常量?
我建议在 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
,您就不必费心在构造函数中设置 class 属性 等等将 $this->storepath
替换为 config
访问器:
$path = $storepath ?? config('app.user_images_dir') . $image_name;
需要注意的是,您有两个可选参数,如果您想更改 $extension
的默认值,您仍然需要在使用 [= 时为 $storepath
提供一个值15=]:
$instanceOfAvatar->make_avatar('foo', null, 'jpeg');
除非您使用 PHP 8 和 named arguments。
尊重所有给出的答案。我想添加我的方法,您可能会觉得有用。
config helper 本身就是一个函数。 PHP中函数的规则是它的默认值必须是绝对值,不能是相对值或函数。换句话说,默认值必须是常量表达式,而不是(例如)变量、class 成员或函数调用。因此,该函数不能使用 config() 包含默认值,因为它是一个函数。但是,您可以做的是稍微调整一下代码。
如果您使用的函数不在 class 中,那么您可以在函数上方定义一个常量,稍后在函数中使用它。
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;
}
如果这个函数在 class 中,那么您可以创建一个常量,然后使用 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;
}
}
我正在将我的旧 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
所以,我的问题是如何像在传统项目中那样使用这些常量?
我建议在 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
,您就不必费心在构造函数中设置 class 属性 等等将 $this->storepath
替换为 config
访问器:
$path = $storepath ?? config('app.user_images_dir') . $image_name;
需要注意的是,您有两个可选参数,如果您想更改 $extension
的默认值,您仍然需要在使用 [= 时为 $storepath
提供一个值15=]:
$instanceOfAvatar->make_avatar('foo', null, 'jpeg');
除非您使用 PHP 8 和 named arguments。
尊重所有给出的答案。我想添加我的方法,您可能会觉得有用。
config helper 本身就是一个函数。 PHP中函数的规则是它的默认值必须是绝对值,不能是相对值或函数。换句话说,默认值必须是常量表达式,而不是(例如)变量、class 成员或函数调用。因此,该函数不能使用 config() 包含默认值,因为它是一个函数。但是,您可以做的是稍微调整一下代码。
如果您使用的函数不在 class 中,那么您可以在函数上方定义一个常量,稍后在函数中使用它。
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;
}
如果这个函数在 class 中,那么您可以创建一个常量,然后使用 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;
}
}