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

· 2009-07-21 (화) 09:26:20 · 2668 · 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건
+
제목 글쓴이 날짜 조회
11-08-16 조회 2,397
11-08-16 조회 2,373
11-08-12 조회 2,212
11-08-02 조회 2,793
11-07-27 조회 2,182
11-07-25 조회 2,311
11-06-28 조회 2,471
11-06-06 조회 3,949
11-06-04 조회 2,572
11-04-25 조회 6,157
11-04-22 조회 2,658
11-04-21 조회 2,850
11-04-08 조회 3,208
11-03-31 조회 2,803
11-03-20 조회 3,205
11-03-10 조회 2,848
11-03-04 조회 3,173
11-03-03 조회 2,895
11-02-26 조회 4,318
11-02-14 조회 5,671
11-02-08 조회 3,278
11-01-30 조회 5,349
11-01-14 조회 5,130
11-01-14 조회 3,716
11-01-14 조회 2,876
11-01-14 조회 2,913
11-01-14 조회 3,060
11-01-12 조회 2,784
11-01-07 조회 4,493
10-12-29 조회 2,404
10-12-22 조회 2,464
10-12-05 조회 5,782
10-11-22 조회 5,201
10-11-13 조회 3,584
10-10-27 조회 3,202
10-10-21 조회 2,902
10-09-21 조회 2,899
10-09-17 조회 2,945
10-09-15 조회 3,393
10-09-14 조회 4,001
10-05-18 조회 2,873
10-02-17 조회 4,308
10-02-12 조회 3,080
09-12-14 조회 5,083
09-12-14 조회 5,992
09-09-19 조회 4,377
09-08-16 조회 6,215
09-07-22 조회 5,628
09-04-13 조회 3,037
09-03-15 조회 5,040
09-03-05 조회 3,871
09-02-09 조회 2,808
08-12-30 조회 3,012
08-12-06 조회 3,216
08-11-30 조회 3,558
08-10-18 조회 4,392
08-10-08 조회 3,679
08-09-19 조회 4,336
08-09-05 조회 2,977
08-09-05 조회 5,082
08-09-05 조회 2,964
08-09-05 조회 3,553
08-09-05 조회 3,472
08-09-01 조회 4,490
08-08-07 조회 2,746
08-06-08 조회 3,218
08-06-02 조회 2,809
08-05-26 조회 5,233
08-05-09 조회 4,300
08-04-22 조회 3,888
08-04-17 조회 4,468
08-04-17 조회 5,555
08-04-17 조회 3,680
08-04-17 조회 8,675
08-04-17 조회 4,559
08-04-17 조회 4,487
08-04-09 조회 3,047
08-03-31 조회 3,543
08-03-26 조회 3,237
08-03-25 조회 3,529
08-03-25 조회 3,957
08-03-25 조회 4,004
08-03-25 조회 3,663
08-03-25 조회 3,554
08-03-25 조회 3,555
08-03-25 조회 5,245
08-02-03 조회 5,223
08-01-18 조회 6,614
08-01-17 조회 3,914
08-01-17 조회 3,733
08-01-12 조회 4,015
08-01-08 조회 3,338
07-12-28 조회 4,615
07-12-04 조회 3,488
07-11-30 조회 5,097
07-11-30 조회 5,713
07-11-02 조회 4,009
07-10-24 조회 3,987
07-10-24 조회 3,260
07-10-24 조회 3,371