如何隐藏特定变量的通知错误?

How do I hide notice error for a specific variable?

我在 class 中有一个可选变量:

private $port = DB_PORT; // The port number where the MySQL is listening.

如果用户没有指定没有定义变量,php会抛出一个通知:

Notice: Use of undefined constant DB_PORT - assumed 'DB_PORT'

我如何修改我的 代码 以告诉 PHP 忽略来自该变量的任何错误?

利用defined功能来处理你要面对的各种情况

if (!defined('DB_PORT')) throw ew MyException("DB_PORT must be configured");