Jump to:

23355 Posts in 17838 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Class Namespace Guidelines?

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

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

Page: 1 2
Go to End
Author Topic: 1830 Views
  • ordinarywebguy
    Avatar
    Community Member
    13 Posts

    Class Namespace Guidelines? Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Class Namespace Guidelines? Link to this post

    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?

  • ordinarywebguy
    Avatar
    Community Member
    13 Posts

    Re: Class Namespace Guidelines? Link to this post

    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.

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Class Namespace Guidelines? Link to this post

    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...

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Class Namespace Guidelines? Link to this post

    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.

  • ordinarywebguy
    Avatar
    Community Member
    13 Posts

    Re: Class Namespace Guidelines? Link to this post

    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?

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Class Namespace Guidelines? Link to this post

    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'.

  • ordinarywebguy
    Avatar
    Community Member
    13 Posts

    Re: Class Namespace Guidelines? Link to this post

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

    Thanks Willr!

    1830 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.