Reputation: 2299
I am having trouble accessing a custom class I created in a new folder in a bundle.
I have a bundle called: MemberBundle - located at src/My/Bundle/MemberBundle
I created a directory called Models located at src/My/Bundle/MemberBundle/Models
In that directory I have a file called MemberModel.php with the following code:
<?php
namespace My\MemberBundle\Models;
class MemberModel {
public function getActiveCampaignId($zone) {
### Custom Mysql Query
...
}
}
When I try and access that class from my controller like this:
$MemberModel = new My\MemberBundle\Models\MemberModel();
$data = $MemberModel->getActiveCampaignId("1");
print_r($data);
I get an error:
Fatal error: Class 'My\MemberBundle\Models\MemberModel' not found in ...
Could anyone please point me in the right direction?
Upvotes: 3
Views: 1478
Reputation: 1401
I tried to attach older class which had underscore in class name and couldn't get the class to load. Apparently underscores are special chars when it comes to class name and are treated as directory separators or something in those lines.
More information @ http://www.sitepoint.com/autoloading-and-the-psr-0-standard/
Each underscore in the class name is converted to a DIRECTORY_SEPARATOR. The underscore has no special meaning in the namespace.
Phew. This took me waaaaay too long to figure it out.
Upvotes: 1
Reputation: 2299
It turns out I was not using the full path as needed. Both paths needed 'Bundle' added into them.
I should have been using these two bits of code:
<?php
namespace My\Bundle\MemberBundle\Models;
class MemberModel {
public function getActiveCampaignId($zone) {
### Custom Mysql Query
...
}
}
And:
$MemberModel = new My\Bundle\MemberBundle\Models\MemberModel();
$data = $MemberModel->getRandomActiveCampaignId("1");
print_r($data);
Upvotes: 1