函数名称:MongoDB\Driver\Monitoring\ServerOpeningEvent::getHost()
适用版本:PHP 7.0及以上
用法: MongoDB\Driver\Monitoring\ServerOpeningEvent::getHost()函数用于获取正在打开的服务器的主机信息。
语法: public function MongoDB\Driver\Monitoring\ServerOpeningEvent::getHost(): MongoDB\Driver\Server
参数:无
返回值: 该函数返回一个MongoDB\Driver\Server对象,该对象包含正在打开的服务器的主机信息。
示例:
<?php
// 创建一个MongoDB\Driver\ServerOpeningEvent对象
$event = new MongoDB\Driver\Monitoring\ServerOpeningEvent();
// 获取正在打开的服务器的主机信息
$host = $event->getHost();
// 输出主机名和端口号
echo "Host: " . $host->getHost() . "\n";
echo "Port: " . $host->getPort() . "\n";
?>
输出:
Host: localhost
Port: 27017
注意事项:
- 该函数需要在MongoDB扩展已安装并启用的情况下使用。
- 如果没有正在打开的服务器,该函数将返回一个空的MongoDB\Driver\Server对象。