获取常量名作为字符串 [英] Get a constant name as a string
问题描述
我为cURL写了一个包装类,当你设置一个选项时,将选项名称保存到数组中,以便我可以管理设置哪些选项。
<问题是,cURL选项名称是常量,它们实际上是整数,所以我不能真正告诉哪些选项已经设置。
从类中节选:
class Curl {
protected $ _options;
public function setOption($ name,$ value){
$ result = curl_setopt($ this-> _handle,$ name,$ value);
if($ result){
$ this-> _options [$ name] = $ value;
}
return $ result;
}
}
假设我设置了以下选项:
array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_HEADER => 0,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 30
)
Curl :: _ options
将如下所示:
array(
(int)78 =>(int)10,
(int)42 =>(int)0,
>(int)1,
(int)19913 =>(int)1,
(int)13 =>(int)30
)
c $ c>
如何获取cURL常量的实际名称?这是否可以使用反射类?
嗯,技术上有一个方法来准备一个数组 curl
常量名称作为字符串及其值as,well,values:
$ curl_constants = get_defined_constants(true)['curl'];
您似乎只处理 CURLOPT
常数,您可以细化:
$ curlopt_constants = [];
foreach($ curl_constants as $ constant_name => $ constant_value){
if(strpos($ constant_name,'CURLOPT')=== 0){
$ curlopt_constants [$ constant_name] = $ constant_value;问题是,这些数组中的几个常量仍然有一个常量相同的值( CURLOPT_SSLCERTPASSWD
, CURLOPT_SSLKEYPASSWD
和 CURLOPT_KEYPASSWD
=> 10026
; CURLOPT_READDATA
和 CURLOPT_INFILE
=> 10009
)。如果你确定这个歧义,你可以翻转这个数组,把它变成一个哈希。 $ curlopt_constants_hash = array_flip $ curlopt_constants);
然后你可以通过查找这个哈希来获得字符串文字:
$ curlopt_constant_name =
isset($ curlopt_constants_hash [$ constant_value])
? $ curlopt_constants_hash [$ constant_value]
:null
;
(检查散列)比使用 array_search
。
I'm writing a wrapper class for cURL which, when you set an option, saves the option name to an array so that I can manage which options are set.
The problem is that with cURL option names being constants, they are actually integers so I can't really tell which options have been set.
Excerpt from the class:
class Curl {
protected $_options;
public function setOption($name, $value) {
$result = curl_setopt($this->_handle, $name, $value);
if ($result) {
$this->_options[$name] = $value;
}
return $result;
}
}
Let's say I set the following options:
array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_HEADER => 0,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 30
)
The Curl::_options
will then look like:
array(
(int) 78 => (int) 10,
(int) 42 => (int) 0,
(int) 47 => (int) 1,
(int) 19913 => (int) 1,
(int) 13 => (int) 30
)
How can I get the actual names of the cURL constants? Is this possible using the Reflection class?
解决方案 Well, technically there's a way to prepare an array with curl
constant names as strings and their values as, well, values:
$curl_constants = get_defined_constants(true)['curl'];
As you seem to deal only with CURLOPT
constants, you can refine it:
$curlopt_constants = [];
foreach ($curl_constants as $constant_name => $constant_value) {
if (strpos($constant_name, 'CURLOPT') === 0) {
$curlopt_constants[$constant_name] = $constant_value;
}
}
The problem is, several constants in these array still have the same value (CURLOPT_SSLCERTPASSWD
, CURLOPT_SSLKEYPASSWD
and CURLOPT_KEYPASSWD
=> 10026
; CURLOPT_READDATA
and CURLOPT_INFILE
=> 10009
). If you're OK with this ambiguity, you can flip this array, turning it into a hash.
$curlopt_constants_hash = array_flip($curlopt_constants);
Then you can get the string literal just by looking in this hash:
$curlopt_constant_name =
isset($curlopt_constants_hash[$constant_value])
? $curlopt_constants_hash[$constant_value]
: null
;
That (checking hash) is faster than using array_search
on the array.
这篇关于获取常量名作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!