我有一个在屏幕上转储变量的函数,我想要做的是在变量值旁边显示变量的名称,所以它会输出如下内容:
-(void) getMapDelta:(CLLocationCoordinate2D) coord
{
MKCoordinateSpan span = self.mapView.region.span;
CLLocationCoordinate2D loc = self.mapView.region.center;
//get latitude in meters
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude - span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude + span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
//get longitude in meters
CLLocation *loc3 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude - span.longitudeDelta * 0.5)];
CLLocation *loc4 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude + span.longitudeDelta * 0.5)];
int metersLatitude = [loc1 distanceFromLocation:loc2];
int metersLongitude = [loc3 distanceFromLocation:loc4];
NSLog(@"Delta> %d / %d", metersLongitude,metersLatitude);
NSLog(@"Coor> %f / %f", coord.latitude,coord.longitude);
}
我该怎么做?
答案 0 :(得分:0)
PHP没有提供这样做的简单方法。 PHP开发人员从来没有看到任何理由为什么这应该是必要的。
但是,您可以使用以下几种解决方法: Is there a way to get the name of a variable? PHP - Reflection和此处:How to get a variable name as a string in PHP?
答案 1 :(得分:0)
在这种情况下,debug_backtrace()
函数可能是最有效的:
function my_function($var) {
$caller = debug_backtrace()[0];
$file = $caller['file'];
$lineno = $caller['line'];
$fp = fopen($file, 'r');
$line = "";
for ($i = 0; $i < $lineno; $i++) {
$line = fgets($fp);
}
$regex = '/'.__FUNCTION__.'\(([^)]*)\)/';
preg_match($regex, $line, $matches);
echo '<pre>'. $matches[1]. ": $var</pre>";
}
$anotherVar = 'Something else';
my_function($anotherVar);
// output: $anotherVar: Something else