PHP, как добавить имя класса или имя файла / путь к файлу до эхо-сообщений - PullRequest
0 голосов
/ 25 октября 2019

В моем проекте у меня есть эхо во многих разных местах. Будет больно редактировать их все. Я хочу добавить [CLASSNAME / FILENAME / FILEPATH] перед эхом, например:

результат эха: NOW:

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

ПОСЛЕ:

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

(если путьиспользование имени класса невозможно (статическое приведение) я хотел бы использовать имя файла или путь к файлу, где используется эхо)

1 Ответ

0 голосов
/ 25 октября 2019

См. Магические константы

Для класс :

 __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.

Для path :

__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!”

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...