Reputation: 5367
I'm just getting started with symfony (2), and I'm trying to port over some code from an existing class - but I'm getting a bit stuck trying to use 'old' paradigms within symfony framework. I have the following code:
namespace AppBundle\Controller;
use AppBundle\Components\model\Customer;
class MyController extends Controller
{
/**
* @Route("/index.php", name="index")
*/
public function indexAction(Request $request)
{
include_once( 'AppBundle\Components\model\Customer.php' );
$entity = new Customer();
$this->get('logger')->info('Instance == > ' . isset($entity) );
$this->get('logger')->info('Class exists == > ' . ( class_exists('Customer') ? 'YES' : 'NO') );
Where Customer.php just contains an empty class called Customer.
However, this code always logs:
[2016-06-24 17:44:42] app.INFO: Instance == > 1 [] []
[2016-06-24 17:44:42] app.INFO: Class exists == > NO [] []
As you can see, I am doing both use
and include_once
.
But it seems I can create an instance, but class_exists
returns false.
To me this seems a contradiction. Can someone tell me how to set this up so that class_exists
returns true.
Upvotes: 1
Views: 608
Reputation: 522145
use
merely establishes an alias so you can refer to the class name using a shorter name. The fully qualified class name is AppBundle\Components\model\Customer
; the use
statement allows you to refer to it simply using Customer
.
However, when referring to classes using a string, you always need to use the fully qualified class name. Aliases established with use
do not apply to strings, since those aren't bound to the lexical scope of use
statements and hence cannot be reliably resolved against them.
This works:
class_exists('AppBundle\Components\model\Customer')
Upvotes: 4