How to restrict a class from creating its instances?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • salsan
    New Member
    • Nov 2012
    • 12

    How to restrict a class from creating its instances?

    Can someone tell me how to restrict a class from creating its instances???
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Restrict it in what way?

    Comment

    • salsan
      New Member
      • Nov 2012
      • 12

      #3
      Restrict from creating the object.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Yes, you've said that. But what is the condition.

        Comment

        • salsan
          New Member
          • Nov 2012
          • 12

          #5
          its properties and methods are actually to use as static.

          Comment

          • shirajulmamun
            New Member
            • Dec 2012
            • 4

            #6
            Declare the class as static, and access the properties and methods through the class.

            Comment

            • salsan
              New Member
              • Nov 2012
              • 12

              #7
              But how do i create a static class in php???

              Code:
              static class StaticClass
              This one didn't work..

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                You don't declare the class as static, you declare the properties and methods as static.

                And you wouldn't be "restrictin g the class from creating instances". Instead you don't instantiate anything and call the functions through the class itself.

                Comment

                • solutionwand
                  New Member
                  • Dec 2012
                  • 16

                  #9
                  Hi,

                  use static keyword before class name.

                  As per php documentation:

                  "Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can)."

                  Comment

                  Working...