如何在PHP中检查特定类型的对象 [英] How to Check for a Specific Type of Object in PHP

查看:72
本文介绍了如何在PHP中检查特定类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法可以接受PDO对象作为参数,以允许用户使用现有的连接,而不是该方法来打开一个新的连接并节省资源:

I have a method which accepts a PDO object as an argument, to allow the user to use an existing connection rather then the method to open a new one, and save resources:

public static function databaseConnect($pdo = null) {

我知道is_object()会检查参数是否是对象,但是我想检查$pdo是否是PDO对象,而不仅仅是对象.

I am aware of is_object() to check if the argument is an object, but I want to check if $pdo is a PDO object, and not just an object.

因为用户可以轻松地输入(错误输入?)另一种对象,例如mysqli之类的东西,并且整个脚本会破裂.

Because the user can easily enter (by mistake?) a different kind of object, a mysqli or such, and the entire script will break apart.

简而言之:如何检查特定类型对象的变量?

In short: How can I check a variable for a specific type of object?

推荐答案

您可以使用 :

if ($pdo instanceof PDO) {
    // it's PDO
}

但是请注意,您不能像!instanceof那样求反,所以​​您应该这样做:

Be aware though, you can't negate like !instanceof, so you'd instead do:

if (!($pdo instanceof PDO)) {
    // it's not PDO
}

另外,查看您的问题,您可以使用对象类型提示,它有助于强制执行要求以及简化检查逻辑:

Also, looking over your question, you can use object type-hinting, which helps enforce requirements, as well as simplify your check logic:

function connect(PDO $pdo = null)
{
    if (null !== $pdo) {
        // it's PDO since it can only be
        // NULL or a PDO object (or a sub-type of PDO)
    }
}

connect(new SomeClass()); // fatal error, if SomeClass doesn't extend PDO

键入的参数可以是必需的或可选的:

Typed arguments can be required or optional:

// required, only PDO (and sub-types) are valid
function connect(PDO $pdo) { }

// optional, only PDO (and sub-types) and 
// NULL (can be omitted) are valid
function connect(PDO $pdo = null) { }

无类型的参数可以通过显式条件实现灵活性:

Untyped arguments allow for flexibility through explicit conditions:

// accepts any argument, checks for PDO in body
function connect($pdo)
{
    if ($pdo instanceof PDO) {
        // ...
    }
}

// accepts any argument, checks for non-PDO in body
function connect($pdo)
{
    if (!($pdo instanceof PDO)) {
        // ...
    }
}

// accepts any argument, checks for method existance
function connect($pdo)
{
    if (method_exists($pdo, 'query')) {
        // ...
    }
}

对于后者(使用 method_exists ) ,我的看法有点复杂.来自Ruby的人会发现 respond_to? 很熟悉, 不论结果好坏.我会亲自编写一个接口并针对该接口执行普通的类型提示:

As for the latter (using method_exists), I'm a bit mixed in my opinion. People coming from Ruby would find it familiar to respond_to?, for better or for worse. I'd personally write an interface and perform a normal type-hint against that:

interface QueryableInterface
{ 
    function query();
}

class MyPDO extends PDO implements QueryableInterface { }

function connect(QueryableInterface $queryable) { }

但是,这总是不可行的;在此示例中,PDO对象不是有效的参数,因为基本类型未实现QueryableInterface.

However, that's not always feasible; in this example, PDO objects are not valid parameters as the base type doesn't implement QueryableInterface.

值得一提的是,值在PHP中具有类型,而不是变量.这很重要,因为null将无法通过instanceof检查.

It's also worth mentioning that values have types, not variables, in PHP. This is important because null will fail an instanceof check.

$object = new Object();
$object = null;
if ($object instanceof Object) {
    // never run because $object is simply null
}

当值变为null时,该值将失去其类型,而类型不足.

The value loses it's type when it becomes null, a lack of type.

这篇关于如何在PHP中检查特定类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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