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!
$obj = new Phone()
should not work either if namespace are related – Lampasuse
ing. Classes in the current namespace aren't resolved when using a string either. – Squiggle