위

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

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

 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건
+
제목 글쓴이 날짜 조회
08-10-30 조회 2,713
08-10-30 조회 2,812
08-10-20 조회 2,541
08-10-11 조회 2,447
08-09-20 조회 4,187
08-09-20 조회 2,857
08-09-18 조회 2,743
08-09-18 조회 3,438
08-09-17 조회 3,301
08-09-05 조회 4,640
08-09-03 조회 2,775
08-08-18 조회 2,900
08-08-17 조회 2,639
08-08-13 조회 3,418
08-07-30 조회 3,483
08-07-28 조회 4,359
08-07-22 조회 5,588
08-07-22 조회 4,076
08-07-10 조회 3,045
08-07-08 조회 3,107
08-06-30 조회 3,867
08-06-30 조회 3,350
08-06-30 조회 3,668
08-06-14 조회 2,741
08-06-11 조회 7,103
08-06-05 조회 3,317
08-05-20 조회 4,829
08-05-13 조회 4,172
08-05-08 조회 3,125
08-05-08 조회 5,279
08-04-25 조회 4,075
08-04-22 조회 4,544
08-04-21 조회 3,187
08-04-17 조회 2,567
08-04-17 조회 2,968
08-04-17 조회 2,264
08-04-17 조회 5,236
08-04-17 조회 4,554
08-04-17 조회 4,548
08-04-17 조회 3,332
08-04-17 조회 3,157
08-04-17 조회 3,642
08-04-17 조회 5,004
08-04-17 조회 5,150
08-04-17 조회 3,357
08-04-04 조회 4,617
08-04-04 조회 2,849
08-04-04 조회 2,591
08-04-04 조회 2,818
08-04-04 조회 2,552
08-04-04 조회 2,796
08-04-03 조회 2,681
08-04-03 조회 2,751
08-04-02 조회 3,064
08-04-02 조회 3,219
08-03-31 조회 3,107
08-03-26 조회 4,285
08-03-11 조회 2,972
08-03-11 조회 2,383
08-03-11 조회 2,395
08-03-07 조회 4,230
08-03-07 조회 3,323
08-03-06 조회 4,771
08-03-04 조회 3,401
08-02-19 조회 3,797
08-02-03 조회 3,419
08-01-18 조회 4,006
08-01-15 조회 3,453
08-01-12 조회 4,300
08-01-12 조회 3,870
08-01-12 조회 3,825
07-12-24 조회 3,118
07-11-30 조회 3,961
07-11-08 조회 5,018
07-11-06 조회 3,871
07-10-26 조회 5,013
07-10-24 조회 4,199
07-10-22 조회 3,585
07-10-19 조회 4,770
07-10-09 조회 4,904
07-10-09 조회 6,769
07-10-05 조회 3,104
07-10-05 조회 4,040
07-10-03 조회 7,548
07-10-03 조회 3,595
07-10-03 조회 2,942
07-10-03 조회 2,871
07-09-28 조회 4,714
07-09-27 조회 3,780
07-09-27 조회 3,230
07-09-13 조회 3,381
07-08-28 조회 3,462
07-08-04 조회 4,851
07-08-01 조회 5,545
07-08-01 조회 4,948
07-07-30 조회 5,469
07-07-29 조회 3,614
07-07-21 조회 3,434
07-07-18 조회 5,393
07-07-16 조회 3,188