Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://www.php.net/manual/en/language.operators.type.php"><code>instanceof</code></a>:</p> <pre><code>if ($pdo instanceof PDO) { // it's PDO } </code></pre> <p>Be aware though, you can't negate like <code>!instanceof</code>, so you'd instead do:</p> <pre><code>if (!($pdo instanceof PDO)) { // it's not PDO } </code></pre> <p>Also, looking over your question, you can use object type-hinting, which helps enforce requirements, as well as simplify your check logic:</p> <pre><code>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 </code></pre> <p>Typed arguments can be required or optional:</p> <pre><code>// 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) { } </code></pre> <p>Untyped arguments allow for flexibility through explicit conditions:</p> <pre><code>// 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')) { // ... } } </code></pre> <p>As for the latter (<em>using <a href="http://php.net/manual/en/function.method-exists.php"><code>method_exists</code></a></em>), I'm a bit mixed in my opinion. People coming from Ruby would find it familiar to <a href="http://ruby-doc.org/core-2.1.1/Object.html#method-i-respond_to-3F"><code>respond_to?</code></a>, for better or for worse. I'd personally write an interface and perform a normal type-hint against that:</p> <pre><code>interface QueryableInterface { function query(); } class MyPDO extends PDO implements QueryableInterface { } function connect(QueryableInterface $queryable) { } </code></pre> <p>However, that's not <em>always</em> feasible; in this example, <code>PDO</code> objects are not valid parameters as the base type doesn't implement <code>QueryableInterface</code>.</p> <p>It's also worth mentioning that <em>values have types</em>, not variables, in PHP. This is important because <code>null</code> will fail an <code>instanceof</code> check.</p> <pre><code>$object = new Object(); $object = null; if ($object instanceof Object) { // never run because $object is simply null } </code></pre> <p>The value loses it's type when it becomes <code>null</code>, a lack of type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload