(originally posted by leepowers in his question)
The error message is confusing for one big reason:
Primitive type names are not reserved in PHP
The following are all valid class declarations:
class string { }
class int { }
class float { }
class double { }
My mistake was in thinking that the error message was referring solely to the string primitive type - the word 'instance' should have given me pause. An example to illustrate further:
class string { }
$n = 1234;
$s1 = (string)$n;
$s2 = new string();
$a = array('no', 'yes');
printf("\$s1 - primitive string? %s - string instance? %s\n",
$a[is_string($s1)], $a[is_a($s1, 'string')]);
printf("\$s2 - primitive string? %s - string instance? %s\n",
$a[is_string($s2)], $a[is_a($s2, 'string')]);
Output:
$s1 - primitive string? yes - string instance? no
$s2 - primitive string? no - string instance? yes
In PHP it's possible for a string
to be a string
except when it's actually a string
. As with any language that uses implicit type conversion, context is everything.