OOP: When should static classes be used?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    OOP: When should static classes be used?

    Simple question: when should static classes be used?
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Originally posted by Markus
    Simple question: when should static classes be used?
    The best way for me to tell you is by example: Date class.

    I've created my own date class that I don't always need to initialize an instance of. Perhaps I need the difference between two dates, a conversion in format.

    The reason I started doing this is because I noticed in my general functions file I had over 10 functions that were all date related and decided to put them in a class and call it when I want.

    echo Date::convertUS toMySQL($date = "04/30/2009", $includeTime = true)
    // outputs: 2009-04-30 00:00:00

    Resource:



    Cheers mate,



    Dan

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by Markus
      Simple question: when should static classes be used?
      if you can't extend it (because you already extended another class) or don't want to have the class stored as property. commonly: if you need methods you can access everywhere (e.g. database classes, error classes, …)

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Thanks, Dan and Dormilich.

        A 'logging' class helped me understand it.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          example: Registry Pattern (text is in German, but the source code is quite self-explanatory)

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            Originally posted by Dormilich
            example: Registry Pattern (text is in German, but the source code is quite self-explanatory)
            This is an idea that I thought about for our Framework, Please remember to bring this up, if I forget. This could replace client.php config file altogether.

            Comment

            Working...