假设我有以下情况:
File1.php:
<?php
require_once('init.php');
...
?>
File2.php:
<?php
require_once('init.php');
...
?>
的init.php:
<?php
magic_function_which_tells_me_which_file_parsed_this_file();
...
?>
我知道这是一个长镜头,但有没有办法从init.php中知道当前执行中哪个文件包含init.php?
答案 0 :(得分:4)
即使没有功能,您也可以使用debug_backtrace
查找来电者:
test1.php
<?php
echo 'test1';
include 'test2.php';
test2.php
<?php
echo 'test2';
print_r(debug_backtrace());
输出
ABCArray
(
[0] => Array
(
[file] => /tmp/b.php
[line] => 3
[function] => include
)
[1] => Array
(
[file] => /tmp/a.php
[line] => 3
[args] => Array
(
[0] => /tmp/b.php
)
[function] => include
)
)
无论如何,我不建议使用它,因为在过度使用时可能会有明显的性能阻力。
答案 1 :(得分:3)
在init.php的顶部,您可以使用debug_backtrace()
来获取有关堆栈的信息。这将告诉您,除其他外,哪个文件包含当前文件,以及在哪一行。
这是回溯输出的示例。如果你把它放在一个函数中,你将有另一层数据。如果你在文件本身中调用它,那么最顶层将告诉你包含该文件的文件。
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_friends = [[NSMutableArray alloc] init];
[self refresh];
}
- (void)refresh {
PFQuery *query = [PFQuery queryWithClassName:@"People"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, error.userInfo);
} else {
[self.collectionView reloadData];
}
}];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
NSLog(@"Do nothing");
} else {
[self performSegueWithIdentifier:@"profilePush" sender:self];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
FriendsTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsTableViewCell" forIndexPath:indexPath];
cell.friendsDelegate = self;
PFObject *friendObj = [_friends objectAtIndex:indexPath.row];
[(PFFile*)friendObj[@"profilePic"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (error) {return;}
cell.profilePic.image = [UIImage imageWithData:data];
}];
cell.nameL.text = friendObj[@"username"];
return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0) {
return 3;
} else {
return _friends.count;
}
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"profilePush"]) {
NSIndexPath *indexPath = (NSIndexPath*)sender;
ProfilePopupViewController *person = segue.destinationViewController;
person.user = [_friends objectAtIndex:indexPath.row];
//.user is a PFObject in the next view controller
}
}
@end
你可以把它包装成一个实用功能:
<?php
$thumbnail_link = 'https://i.ytimg.com/vi_webp/s4bw0HQotfU/0.jpg';
if ( function_exists('curl_init') )
{
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $thumbnail_link);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
echo $image; //this will return an empty data why is it?
// create & save image;
$img_res = @imagecreatefromstring($image);
if($img_res === false)
return FALSE;
$img_width = imagesx($img_res);
$img_height = imagesy($img_res);
$resource = @imagecreatetruecolor($img_width, $img_height);
if( function_exists('imageantialias'))
{
@imageantialias($resource, true);
}
@imagecopyresampled($resource, $img_res, 0, 0, 0, 0, $img_width, $img_height, $img_width, $img_height);
@imagedestroy($img_res);
switch($ext)
{
case ".gif":
//GIF
@imagegif($resource, $upload_path . $thumb_name);
break;
case ".jpg":
//JPG
@imagejpeg($resource, $upload_path . $thumb_name);
break;
case ".png":
//PNG
@imagepng($resource, $upload_path . $thumb_name);
break;
}
}
?>
答案 2 :(得分:3)
当然可以。使用debug_print_backtrace()
。
#0 require_once()在[C:\ xampp \ htdocs \ file2.php:3]中调用
#1 require_once(C:\ xampp \ htdocs \ file2.php)在[C:\ xampp \ htdocs \ file1.php:3]中调用
这会告诉您init.php
已file2.php
列在3
行。{/ p>
答案 3 :(得分:0)
您也可以尝试使用变量来实现此目的。 我们将它命名为$ parentFile:
$parentFile = basename(__FILE__);
require('some.file.here.php');
在some.file.here.php中:
if($parentFile == 'another.file.php')
// do something;
答案 4 :(得分:0)
我会提出一个答案 - 显然所有的功劳都归功于那些已经在我面前回答这个问题的人。
我所做的是将debug_backtrace输出格式化为错误日志:
$debug = debug_backtrace(2 , 16);
error_log('-------------------------------' );
foreach ( $debug as $error ) {
error_log( str_pad( $error['file' ], 120 ) . str_pad($error ['line'] , 8) . $error['function' ] );
}
结果将是每行一个文件,包含(文件,行,函数)表格方式。