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

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

 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,212
16년 전 조회 2,867
16년 전 조회 1,748
16년 전 조회 3,599
16년 전 조회 4,441
16년 전 조회 3,105
16년 전 조회 2,352
16년 전 조회 2,434
16년 전 조회 2,683
16년 전 조회 2,171
16년 전 조회 5,512
16년 전 조회 3,366
16년 전 조회 1,360
16년 전 조회 1,701
16년 전 조회 5,076
16년 전 조회 2,310
16년 전 조회 3,413
16년 전 조회 2,646
16년 전 조회 1,565
16년 전 조회 5,359
16년 전 조회 2,476
16년 전 조회 5,967
16년 전 조회 1,943
16년 전 조회 4,016
16년 전 조회 3,034
16년 전 조회 2,217
16년 전 조회 2,237
16년 전 조회 4,405
16년 전 조회 3,300
16년 전 조회 2,836
16년 전 조회 2,972
16년 전 조회 2,089
16년 전 조회 1,770
16년 전 조회 1,682
16년 전 조회 1,404
16년 전 조회 1,678
16년 전 조회 1,904
16년 전 조회 1,598
16년 전 조회 4,847
16년 전 조회 3,828
16년 전 조회 1,795
16년 전 조회 1,539
16년 전 조회 2,275
16년 전 조회 4,520
16년 전 조회 3,548
16년 전 조회 2,583
16년 전 조회 4,344
16년 전 조회 3,206
16년 전 조회 1,354
16년 전 조회 1,377
16년 전 조회 2,039
16년 전 조회 1,884
16년 전 조회 2,565
16년 전 조회 2,236
16년 전 조회 1,366
16년 전 조회 4,385
16년 전 조회 1,481
16년 전 조회 1,785
16년 전 조회 2,210
16년 전 조회 3,844
16년 전 조회 2,739
16년 전 조회 1,493
16년 전 조회 4,014
16년 전 조회 1,396
16년 전 조회 1,580
16년 전 조회 1,248
16년 전 조회 1,720
16년 전 조회 1,620
16년 전 조회 1,634
16년 전 조회 1,423
16년 전 조회 2,178
16년 전 조회 1,821
16년 전 조회 1,975
16년 전 조회 2,187
16년 전 조회 1,473
16년 전 조회 2,309
16년 전 조회 2,257
16년 전 조회 4,535
16년 전 조회 1,297
16년 전 조회 1,051
16년 전 조회 2,010
16년 전 조회 1,980
16년 전 조회 1,512
16년 전 조회 2,038
16년 전 조회 2,279
16년 전 조회 1,393
16년 전 조회 1,731
16년 전 조회 1,925
16년 전 조회 1,015
16년 전 조회 1,691
16년 전 조회 1,410
16년 전 조회 1,912
16년 전 조회 1,329
16년 전 조회 2,207
16년 전 조회 1,894
16년 전 조회 5,874
16년 전 조회 1,919
16년 전 조회 3,688
16년 전 조회 1,467
16년 전 조회 1,757
🐛 버그신고