PHP如何在回显消息之前添加类名或文件名/文件路径

问题描述 投票:0回答:1

在我的项目中,我在很多不同的地方都有回声。编辑所有这些都是很痛苦的。我希望在回显之前添加[CLASSNAME / FILENAME / FILEPATH],例如:

回显结果:现在:

message: test1, asd!
error: file does not exist!

之后:

[SuperClass] message: test1, asd!
[OtherClass] error: file does not exist!

((如果无法使用类名的方式(静态转换),我想在使用回声的情况下使用文件名或文件路径)

php logging echo
1个回答
0
投票

请参见Magic constants

对于class是:

 __CLASS__  

The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.

对于文件是:

__FILE__    

The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.

对于路径

__DIR__ 

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.

这是一个示例,说明如何使用魔术常数包含类而不在每个回显中写入每个类:

echo “[”.__CLASS__.”] message: test1, asd!”

© www.soinside.com 2019 - 2024. All rights reserved.