0

In PHP, the baseline class is stdClass, and all further classes are children of that class, and it also is the default object returned in most cases where there is no other class defined.

My question is first if there is an equivalent system defined equivalent interface, like stdInterface or something similar?

Secondly, is there a system defined generic trait? I would expect this to be less likely, but I wouldn't rule it out without some confirmation.

I am not aware of either such construct, and could not find any particular reference to either or even this question being asked before. It might be useful in some cases if one or both were available. Thanks in advance.

Edit: As pointed out by deceze and mentioned elsewhere also, stdClass is not the root class, but is just a generic object.

mopsyd
  • 1,877
  • 3
  • 20
  • 30
  • Since interfaces and traits cannot be instantiated by themselves, when would you ever need to test if an interface is an `instanceof` some other (root) interface? – deceze Dec 11 '17 at 11:00
  • To avoid the weight of reflection, and check if a localized extension of a common interface exists, such as someone creating an interface that inherits a Psr standard and adds additional package specific functionality or something. There are some cases where I would like to know what a packages realm of responsibility is without instantiating an object or firing a reflector if it can be avoided. – mopsyd Dec 11 '17 at 11:07
  • Eg figuring out if a problem can be handled in a very large project with an asset already added as a dependency of some other package by Composer, without having to add yet another package and bloat out disk space further. It would be pretty heavy to scrape through this in some cases, and I'd rather save some memory when possible. – mopsyd Dec 11 '17 at 11:09

2 Answers2

1

There is no stdInterface or stdTrait in PHP but you can use interface_exists and trait_exists to test if a variable is an interface or a trait, ex:

<?php
trait X {}
interface Y {}

var_export(interface_exists('Y'));
echo '<br>';
var_export(trait_exists('X'));
//Output:
//true
//true
T. AKROUT
  • 1,719
  • 8
  • 18
  • In most cases those and similar functions will fail if the related construct has not yet compiled at the current runtime. Using `instanceof` will generally force `spl_autoload` to fire if it has not already, hence the preference. – mopsyd Dec 11 '17 at 10:56
  • Those are viable options though, so I'll still upvote your answer. – mopsyd Dec 11 '17 at 10:57
1

As a result of this, $object instancof \stdClass will always evaluate to true.

That is entirely and demonstrably wrong.

PHP does not have a rigid top-down hierarchy of anything, there is no top-level class, and also no top-level interface or trait.

You can use is_subclass_of to determine whether a class or interface is related to another class or interface without needing an instance of it:

interface  Foo {}

class Bar implements Foo {}

interface Baz extends Foo {}

var_dump(is_subclass_of('Bar', 'Foo'));  // true
var_dump(is_subclass_of('Baz', 'Foo'));  // true
deceze
  • 510,633
  • 85
  • 743
  • 889