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


How do I hide notice error for a specific variable?

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

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

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

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

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

使用定义的函数来处理您必须面对的各种情况。

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