简体   繁体   English

PHP函数名称中的变量

[英]variable in php function name

I'm wanting to utilise PHP's built-in imagejpeg and imagepng functions. 我想利用PHP的内置imagejpegimagepng函数。 I have a variable $type which defines the filetype jpeg or png. 我有一个变量$type ,它定义文件类型jpeg或png。

I wanted to have one function call. 我想要一个函数调用。 Something like this: 像这样:

image.$type($image, null, 100);

But that doesn't work. 但这是行不通的。 Any ideas? 有任何想法吗?

It should works if you use the full function name in a var 如果您在var中使用完整的函数名称,则应该可以使用

$func = "image".$type;
$func($image,null,100);

Careful : imagepng use a quality between 0-9 (not 0 and 100) 注意imagepng使用0-9之间的质量(而不是0和100)

for completeness: you should use an approach more solid, using a OOP method with Factory method pattern . 为了完整性:您应该使用更可靠的方法,将OOP方法与Factory方法模式一起使用 However I don't know your situation and your code. 但是我不知道您的情况和您的代码。 So it's just a note. 因此,这只是一个注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM