3.10. parent:: and self::

PHP supports two reserved class names that make it easier when writing OO applications. self:: refers to the current class and it is usually used to access static members, methods, and constants. parent:: refers to the parent class and it is most often used when wanting to call the parent constructor or methods. It may also be used to access members and constants. You should use parent:: as opposed to the parent's class name because it makes it easier to change your class hierarchy because you are not hard-coding the parent's class name.

The following example makes use of both parent:: and self:: for accessing the Child and Ancestor classes:

 class Ancestor { const NAME = "Ancestor"; function __construct() { print "In ...

Get PHP 5 Power Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.