以下是一些PHP中常用的类及其用途的实例:
| 类名 | 用途 | 实例代码 |
|------------|--------------------------------------------------------------|----------------------------------------------------------------|
| DateTime | 处理日期和时间 | `$date = new DateTime();
echo $date->format('Y-m-d H:i:s');` |
| PDO | 数据库访问,支持多种数据库 | `$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'username', 'password');` |
| mysqli | MySQL数据库访问 | `$mysqli = new mysqli('localhost', 'username', 'password', 'testdb');` |
| stdClass | 创建自定义对象 | `$obj = new stdClass();
$obj->name = 'John Doe';` |
| SimpleXML | XML处理 | `$xml = simplexml_load_string('
| DOMDocument| XML处理 | `$dom = new DOMDocument();
$dom->loadXML('
| Exception | 异常处理 | `
try {
throw new Exception('Error message');
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "