Reputation: 8184
I'm using the symfony2 framework and I want to use the PHP's DateTime class (PHP version is 5.3).
Here the declaration:
namespace SDCU\GeneralBundle\Entity;
class Country
{
public function __construct(){
$this->insertedAt = new DateTime();
}
}
But, when executing this constructor, I get an error saying that there's no "SDCU\GeneralBundle\Entity\DateTime" class. I've been searching around for DateTime's namespace but with no success... any idea?
Upvotes: 59
Views: 67610
Reputation: 44999
DateTime
is in the global namespace, and as "class names always resolve to the current namespace name" you have to use \DateTime
.
Or import the package using:
use \Datetime;
Upvotes: 105
Reputation: 43572
Better solution for using classes in global namespaces is "use" keyword instead of "\" before class.
namespace SDCU\GeneralBundle\Entity;
use \DateTime;
class Country
{
public function __construct(){
$this->insertedAt = new DateTime();
}
}
Upvotes: 23