Can a POST value ever be an instanceof something in PHP?

Viewed 36

I've come across PHP code with the a check which tests if a POST value is an instanceof a class:

if ($_POST['something'] instanceof SomeClass) {
    // do something
}

This seems odd to me, because I wouldn't think that the check can ever be true. A POST value is a string after all, and a string isn't an instance of a class.

I tried passing the serialized version of an instance (O:9:"SomeClass":0:{}), but that doesn't work (which makes sense, as it's still a string, not an object).

Am I correct in thinking that this check can never be true? Or am I missing something here?

1 Answers

I think this is not a simple question. I think theoretically "yes".

As a $_POST is a array of variables, and there is no limitation what the elements of a array can be, you can for example create a array of objects.

$array[] = new stdClass;
$array[0]->variable = value;
etc ...

You see the code author is checking if $_POST['something'], that is a element of the array, and that easily can be a object, is a instance of a class.

Now I have not tested it, but theoretically one could put a object of a class in a array and send it via $_POST nicely encoded.

Related