Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Last active December 19, 2015 01:19
Show Gist options
  • Save mnapoli/5874705 to your computer and use it in GitHub Desktop.
Save mnapoli/5874705 to your computer and use it in GitHub Desktop.
PHP 5.5 ::class feature
<?php
use A\Long\Namespace\User;
use Some\Namespace\ProductService;
// Doctrine: get entity
$user = $entityManager->find('A\Long\Namespace\User', 1234);
$user = $entityManager->find(User::class, 1234);
// Doctrine repositories
$repository = $em->getRepository('A\Long\Namespace\User');
$repository = $em->getRepository(User::class);
// Doctrine Query Builder
$qb->select('u')
->from('A\Long\Namespace\User', 'u')
->where('u.id = ?1')
->orderBy('u.name', 'ASC');
$qb->select('u')
->from(User::class, 'u')
->where('u.id = ?1')
->orderBy('u.name', 'ASC');
// DI container
$container->get('Some\Namespace\ProductService');
$container->get(ProductService::class);
// Another with DI container
$container->set('foo')
->bindTo('Some\Namespace\ProductService')
->withArguments(/* ... */);
$foo = $container->get('foo');
$container->set('foo')
->bindTo(ProductService::class)
->withArguments(/* ... */);
$foo = $container->get('foo');
// PHPUnit
$this->assertInstanceOf('A\Long\Namespace\User', $foo);
$this->assertInstanceOf(User::class, $foo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment