Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Class Namespace Guidelines?


Go to End


9 Posts   5227 Views

Avatar
ordinarywebguy

Community Member, 13 Posts

21 July 2010 at 2:00am

I'm new w/ SilverStripe and I've encountered problems w/ class namespacing. I was following the tutorials in the book "Silverstripe The Complete Guide to CMS Development" and got this error:

Warning: There are two files both containing the same class: 'C:\wamp\www\silverstripe/mysite/code/Member.php' and 'C:\wamp\www\silverstripe/sapphire/security/Member.php'. This might mean that the wrong code is being used. in C:\wamp\www\silverstripe\sapphire\core\ManifestBuilder.php on line 461

Maybe atleast silvestripe can implement guidelines w/ it especially when creating new modules to avoid the said error.

Avatar
swaiba

Forum Moderator, 1899 Posts

21 July 2010 at 2:11am

no no - the code in sapphire/core/ManifestBuilder.php works - my guess is that you have created this Member.php file and it contains

class Member ...

and silverstripe is rightly going to insist that you have only one Member class. Are you trying to "Decorate" the member class?

Avatar
ordinarywebguy

Community Member, 13 Posts

21 July 2010 at 4:54pm

For example I'll create a new module let's say "Google". Then there'll be a Map.php class. Then another developer is creating another module "Yahoo". The developer might also create a Map.php class. So there's a conflict and to prevent it, GoogleMap.php class and YahooMap.php class are advisable naming convention.

As I can see with Silverstripe's package modules like cms, it doesn't follow the said convention above. And the chance of class naming conflict is high.

Avatar
swaiba

Forum Moderator, 1899 Posts

21 July 2010 at 5:41pm

I disagree I have built several larger than average functionality sites and only had this issue once when I wanted to name a file/class Session.php in the end I should have gone for BookingSession which was a better name.

In general [programming hat on] you should give things as desciptive names as possible - so using 2/3 words for each and say there is a conflict because you want a Car DataObject and a Car ModelAdmin... well they clearly should not both be called Car.php...

Avatar
Willr

Forum Moderator, 5523 Posts

21 July 2010 at 6:41pm

We don't have any specific guidelines for modules etc but for core libraries if a class is likely going to conflict (such as Datetime) it should be named spaced with SS_.

For modules which need classes like 'Map' really its the responsibility of the module author to come up with a practical name such as GoogleMap.php etc.

We also encourage the usage of naming a class with what functionality it provides which makes it clearer for developers and reduces conflicts such as

Car
CarController
CarAdmin

To define classes for those.

For decorating members I normally do class it something like ForumMember (where Forum is the name of the module which it appends it to) or SiteNameMember (where Site name is the name of your site).

If you would like this documented more strictly I would be happy to provide a wikipedia to help you decide. Add it as a documentation request on open.silverstripe.org.

Avatar
ordinarywebguy

Community Member, 13 Posts

21 July 2010 at 6:45pm

Yeah! It's true that's my main point (descriptive class name) like my example above. So what's the good class name for Car DataObject and a Car ModelAdmin? You had even experience that problem and as a newbie I would like to read some guidelines on it. :)

And oh yeah I would like to see Silverstripe on PHP 5.3 utilized namespace feature. Are there already actions on it?

Avatar
swaiba

Forum Moderator, 1899 Posts

21 July 2010 at 6:50pm

For Car DataObject I tend to use Car.php because it defines a Car.
For Car ModelAdmin I use CarAdmin.php (which then contains the controller) because it administers the 'Cars'.

Avatar
ordinarywebguy

Community Member, 13 Posts

21 July 2010 at 6:52pm

Yeah it would be better if we have the wiki for this. :)

Thanks Willr!

Go to Top