Tuesday, 15 January 2013

Get the name of a constant passed as a parameter in PHP -



Get the name of a constant passed as a parameter in PHP -

i'm building object-oriented wrapper around curl , have created setter curlopt_ constants stores $property => $value array.

public function setcurlopt($optcode, $optvalue) { $ch = $this->ch; curl_setopt($ch, $optcode, $optvalue); $this->curlopts[$optcode] = $optvalue; } $curler->setcurlopt(curlopt_followlocation, 1);`

however, $optcode not valid index array, since references resource id instead of name of constant.

in order create work, need able name of constant. there way along lines of in php:

function getconstantname($fromvariable) { ... homecoming $constantname; } $x = constant_y; echo getconstantname($x);

output: constant_y

once constant assigned variable there no way find out constant used in assignment. php copies constant value variable.

only viable alternative utilize combination of defined() , constant() , pass alternative name after curlopt_

public function setcurlopt($optcode, $optvalue) { if (defined("curlopt_" . $optcode) === true) { $ch = $this->ch; curl_setopt($ch, constant("curlopt_" . $optcode), $optvalue); $this->curlopts[constant("curlopt_" . $optcode)] = $optvalue; } } $curler->setcurlopt("followlocation", 1);

php

No comments:

Post a Comment