The static keyword is used to declare fields
and methods that can be accessed without having to create an instance of the class. Static members exists in only one copy which
belongs to the class itself, whereas instance members are created as new copies for each
object. This means that static methods cannot use
instance members since these methods are not part of any instance. They can however use
static members by prefixing them with the self keyword followed by a double colon. The
self keyword is an alias for the class name, so we can alternatively use the actual name
of the class. Instance methods can in addition to instance
members also access static members in the same way. This syntax is also used from outside the
class in order to access static members. The advantage of static members can be seen
here in that we can use them without having to create an instance of the class. Methods
should therefore be declared static if they performs a generic function independently
of instance variables and fields can be declared static if we only want a single instance. Another field modifier that can be good to
know about is const, to make a field unchangeable. These constant fields must be set to a constant
value at the same time as they are declared. They don’t use the dollar symbol, but are
otherwise accessed in the same way as static members.

PHP Tutorial – 13 – Static and Constants
Tagged on:                                                                                                                                                         

One thought on “PHP Tutorial – 13 – Static and Constants

  • October 24, 2012 at 2:19 pm

    Why do you talk like this?


Leave a Reply

Your email address will not be published. Required fields are marked *