PHP名称空间和内置类的问题,如何解决? [英] Problems with PHP namespaces and built-in classes, how to fix?

查看:55
本文介绍了PHP名称空间和内置类的问题,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP编写一个小型库,但由于无法读取内置类而遇到了一些问题.例如:

I'm writing a small library in PHP and i'm having some problems with built-in classes not being read. For example:

namespace Woody;

class Test {
  public function __construct() {
    $db = new PDO(params);
  }
}

这给了我:

PHP致命错误:在/var/www/test.php中找不到类'Woody \ PDO'

PHP Fatal error: Class 'Woody\PDO' not found in /var/www/test.php

推荐答案

此:

namespace Woody;
use PDO;

或者:

$db = new \PDO(params);

以防万一是,类PDO不是您的命名空间中的全限定名,因此PHP会查找不可用的Woody\PDO.

Point in case is, that the class PDO is not a full qualified name within your Namespace, so PHP would look for Woody\PDO which is not available.

请参见名称解析规则 文档 详细说明如何将类名称解析为完全限定的名称.

See Name resolution rulesDocs for a detailed description how class names are resolved to a Fully qualified name.

这篇关于PHP名称空间和内置类的问题,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆