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

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

 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개

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

프로그램

+
제목 글쓴이 날짜 조회
16년 전 조회 2,773
16년 전 조회 3,428
16년 전 조회 2,286
16년 전 조회 4,184
16년 전 조회 4,979
16년 전 조회 3,623
16년 전 조회 2,870
16년 전 조회 2,995
16년 전 조회 3,208
16년 전 조회 2,726
16년 전 조회 6,079
16년 전 조회 3,901
16년 전 조회 1,889
16년 전 조회 2,227
16년 전 조회 5,589
16년 전 조회 2,835
16년 전 조회 3,918
16년 전 조회 3,192
16년 전 조회 2,091
16년 전 조회 5,915
16년 전 조회 3,021
16년 전 조회 6,505
16년 전 조회 2,486
16년 전 조회 4,522
16년 전 조회 3,535
16년 전 조회 2,719
16년 전 조회 2,753
16년 전 조회 4,876
16년 전 조회 3,815
16년 전 조회 3,338
16년 전 조회 3,495
16년 전 조회 2,611
16년 전 조회 2,282
16년 전 조회 2,194
16년 전 조회 1,909
16년 전 조회 2,195
16년 전 조회 2,397
16년 전 조회 2,108
16년 전 조회 5,358
16년 전 조회 4,348
16년 전 조회 2,296
16년 전 조회 2,039
16년 전 조회 2,810
16년 전 조회 5,046
16년 전 조회 4,066
16년 전 조회 3,092
16년 전 조회 4,837
16년 전 조회 3,683
16년 전 조회 1,836
16년 전 조회 1,869
16년 전 조회 2,543
16년 전 조회 2,377
16년 전 조회 3,083
16년 전 조회 2,696
16년 전 조회 1,839
16년 전 조회 4,887
16년 전 조회 1,975
16년 전 조회 2,283
16년 전 조회 2,710
16년 전 조회 4,357
16년 전 조회 3,253
16년 전 조회 1,971
16년 전 조회 4,511
16년 전 조회 1,874
16년 전 조회 2,057
16년 전 조회 1,723
16년 전 조회 2,198
16년 전 조회 2,122
16년 전 조회 2,112
16년 전 조회 1,887
16년 전 조회 2,654
16년 전 조회 2,282
16년 전 조회 2,443
16년 전 조회 2,675
16년 전 조회 1,939
16년 전 조회 2,827
16년 전 조회 2,717
16년 전 조회 5,013
16년 전 조회 1,804
16년 전 조회 1,543
16년 전 조회 2,486
16년 전 조회 2,440
16년 전 조회 2,015
16년 전 조회 2,555
16년 전 조회 2,748
16년 전 조회 1,856
16년 전 조회 2,236
16년 전 조회 2,410
16년 전 조회 1,491
16년 전 조회 2,200
16년 전 조회 1,881
16년 전 조회 2,383
16년 전 조회 1,780
16년 전 조회 2,678
16년 전 조회 2,376
16년 전 조회 6,359
17년 전 조회 2,378
17년 전 조회 4,140
17년 전 조회 1,901
17년 전 조회 2,204