I'm trying to instantiate a class (a Laravel3 Eloquent model) by a variable, and I'm getting an error saying that the class is not found.
When I hardcode the class name, though, it works just fine.
(FYI, in the code below $contact_type
is expected to either be Phone, Fax, or Email.)
Here's what I'm playing with at the moment:
foreach( $input AS $contact_type => $contact_info )
{
foreach( $contact_info AS $data )
{
$obj = new $contact_type( (array)$data);
echo'<pre>Obj: ',print_r($obj),'</pre>'; // <----- For Testing
}
}
When I run the code as above, it throws a "Class 'Phone' not found" error.
When I replace new $contact_type()
with new Phone()
(or Fax or Email), it works just fine.
I bet there's something simple I'm just looking over :) What am I missing?
Please help!
The relevant manual entries that covers this are here and here. To quote the second link:
This works for me:
Produces the output:
Look to make sure you're not in a namespace (include the Phone class' namespace in the string if you are). Or, you can also try using reflection:
If the Phone class is in a namespace, this also works:
Phone.php
test.php
I'm not 100% sure why, although I suspect that when evaluating a variable class name the current namespace mappings aren't visible. Consider this code:
Compare that with:
When PHP decides to allocate memory for a new instance, how will it know to map the class name alias of
WeirdName
toSomePackage\Someclass
? That alias is only valid for the current file, and the code that actually performs that operation isn't even in userland code, much less the same file.