Sunday, November 14, 2010

PHP :: Using the scope resolution operator



The scope resolution operator is a pair of colons (::). The name of the class goes on
the left side of the operator, and the name of the method or property goes on the right
like this:

ClassName::methodOrPropertyName

PHP doesn't object if you put whitespace on either side of the scope resolution operator,
but the normal convention is to write everything as a single entity.

The scope resolution operator has the following uses:

  • It gives access to overridden properties or methods of a parent class.
  • It is used to call the static methods and properties of a class (this is covered in "Creating static properties and methods" later in this chapter).
  • It gives access to class constants (see "Using class constants for properties" later in the chapter).
  • It is the conventional way of referring to a class method or property in documentation. For example, instead of referring to "the getPageCount() method of the Ch2_Book class," this is shortened to Ch2_Book::getPageCount().
However, PHP provides two handy keywords that work with the scope resolution operator,
namely:
parent: This refers to the parent or any ancestor of the current class.
self: This refers to the current class. Although this sounds the same as $this,
which you met earlier, self refers to the class in general, whereas $this refers to
the current instance of the class (in other words, an object created from the class).

The advantage of the parent and self keywords is that you don't need to change them if
you rename any of the classes to which they refer

Double Dot Means what :: - PHP answers

Double Dot Means what :: - PHP answers:

You could think of it as, just calling that function w/o instantiating
the class that it's in