Check if a string contains a valid class name
Posted: Sun Aug 06, 2006 4:36 pm
I tried this:
But i got "Fatal error: Invalid opcode 138/1/1" which I was quite proud of actually.
Interestingly if I use self instead of __CLASS__ it complains that self is an undefined constant!
I've found that this:
Will generate an E_FATAL if the class doesn't exist, but also it also generates errors if the constructor is called with wrong params.
So is reflection my only option?
Edit:Does a find job, I was worried it was going to be hard 
Code: Select all
class A
{
}
class B {
public static function testInstanceOf($enforceType)
{
return __CLASS__ instanceof $enforceType;
}
}
var_dump(B::testInstanceOf('A')); // does A exist as a class?Interestingly if I use self instead of __CLASS__ it complains that self is an undefined constant!
I've found that this:
Code: Select all
class A
{
}
class B {
public static function testInstanceOf($enforceType)
{
$a = new $enforceType();
}
}
var_dump(B::testInstanceOf('notaclass'));So is reflection my only option?
Edit:
Code: Select all
$reflection = new ReflectionClass($enforceType);