넘겨온 변수의 이름을 추출하는 함수..

· 2009-07-21 (화) 09:26:20 · 2666 · 1

값이아니라 변수명입니다~

 Here s a function to get the name of a given variable.  Explanation and examples below.

   function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
   {
     if($scope) $vals = $scope;
     else      $vals = $GLOBALS;
     $old = $var;
     $var = $new = $prefix.rand().$suffix;
     $vname = FALSE;
     foreach($vals as $key => $val) {
       if($val === $new) $vname = $key;
     }
     $var = $old;
     return $vname;
   }


 Explanation:

 The problem with figuring out what value is what key in that variables scope is that several variables might have the same value.  To remedy this, the variable is passed by reference and its value is then modified to a random value to make sure there will be a unique match.  Then we loop through the scope the variable is contained in and when there is a match of our modified value, we can grab the correct key.

 Examples:

 1.  Use of a variable contained in the global scope (default):
 < ?php
   $my_global_variable = "My global string.";
   echo vname($my_global_variable); // Outputs:  my_global_variable
 ? >

 2.  Use of a local variable:
 < ?php
   function my_local_func()
   {
     $my_local_variable = "My local string.";
     return vname($my_local_variable, get_defined_vars());
   }
   echo my_local_func(); // Outputs: my_local_variable
 ? >

 3.  Use of an object property:
 < ?php
   class myclass
   {
     public function __constructor()
     {
       $this->my_object_property = "My object property  string.";
     }
   }
   $obj = new myclass;
   echo vname($obj->my_object_property, $obj); // Outputs: my_object_property
 ? >
 =>2줄로
 < ?php
 function var_name(&$var, $scope=0)
 {
     $old = $var;
     if (($key = array_search($var = 'unique'.rand().'value', !$scope ? $GLOBALS : $scope)) && $var = $old) return $key;
 }
 ? >

[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]
|

댓글 1개

찾다찾다 여기서 찾게 되었네요. 정말 감사합니다.
그러고보니 딱 일년전 글이군요 ㅎ
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
10-05-31 조회 2,873
10-04-17 조회 4,680
10-03-23 조회 5,828
10-02-26 조회 3,348
10-02-23 조회 3,312
10-02-11 조회 3,932
10-02-09 조회 2,994
10-01-26 조회 3,448
10-01-23 조회 6,773
10-01-19 조회 3,490
10-01-13 조회 3,695
09-12-06 조회 4,584
09-11-24 조회 4,170
09-11-19 조회 3,048
09-11-11 조회 3,648
09-08-30 조회 3,124
09-08-12 조회 3,129
09-07-21 조회 3,304
09-07-19 조회 3,185
09-07-11 조회 3,101
09-06-15 조회 2,875
09-05-29 조회 3,061
09-05-28 조회 3,487
09-05-05 조회 3,341
09-04-07 조회 3,561
09-02-23 조회 4,356
09-02-17 조회 3,098
09-02-17 조회 2,478
09-02-09 조회 2,576
09-01-23 조회 3,378
09-01-17 조회 4,212
09-01-08 조회 3,162
08-11-07 조회 4,425
08-11-01 조회 4,911
08-10-30 조회 3,505
08-10-16 조회 3,423
08-10-02 조회 2,560
08-09-22 조회 3,780
08-09-22 조회 2,882
08-09-20 조회 5,377
08-09-20 조회 4,432
08-09-20 조회 3,511
08-09-20 조회 2,888
08-09-20 조회 3,081
08-09-20 조회 4,067
08-09-20 조회 3,557
08-09-20 조회 3,139
08-09-18 조회 3,378
08-09-06 조회 2,980
08-09-06 조회 2,738
08-09-05 조회 3,392
08-09-05 조회 5,600
08-09-03 조회 4,405
08-09-03 조회 2,653
08-07-31 조회 3,143
08-07-18 조회 3,750
08-06-19 조회 3,094
08-06-15 조회 3,077
08-06-14 조회 3,532
08-05-23 조회 2,770
08-05-13 조회 2,681
08-04-22 조회 4,346
08-04-22 조회 5,049
08-04-22 조회 6,794
08-04-17 조회 2,972
08-04-17 조회 3,541
08-04-17 조회 3,649
08-04-17 조회 5,895
08-04-17 조회 3,093
08-04-17 조회 3,424
08-04-06 조회 3,512
08-04-05 조회 3,094
08-04-04 조회 7,150
08-04-04 조회 4,442
08-04-04 조회 3,793
08-04-04 조회 3,960
08-04-04 조회 2,843
08-04-04 조회 2,633
08-04-04 조회 2,681
08-04-04 조회 2,869
08-04-04 조회 2,625
08-04-04 조회 2,586
08-04-04 조회 4,104
08-04-03 조회 2,624
08-04-03 조회 3,836
08-04-02 조회 3,055
08-04-02 조회 2,805
08-03-25 조회 3,663
08-03-11 조회 3,427
08-03-11 조회 2,937
08-03-11 조회 2,897
08-03-11 조회 2,694
08-03-11 조회 2,468
08-03-11 조회 2,273
08-03-11 조회 2,327
08-03-07 조회 2,863
08-03-05 조회 2,604
08-02-27 조회 2,836
08-02-21 조회 2,721
08-02-21 조회 3,293