Programming Tutorials

parent:: AND self:: in PHP

By: Andi, Stig and Derick in PHP Tutorials on 2008-11-22  

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 " . self::NAME . " constructor\n";

class Child extends Ancestor {
const NAME = "Child";
function __construct()
print "In " . self::NAME . " constructor\n";

$obj = new Child();

The previous example outputs

In Ancestor constructor
In Child constructor

Make sure you use these two class names whenever possible.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in PHP )

parent:: AND self:: in PHP

Malware: global $ob_starting;

Building PHP with Apache2 on SuSE Professional 9.1/9.2

Installing PHP with Apache 2.x on HP UX 11i and configuring PHP with Oracle 9i

Cannot load /usr/local/apache/libexec/ into server:

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Installing PHP with nginx-server under windows

Warning: session_start(): open .... failed - PHP error

Convert IP address to integer and back to IP address in PHP

Function to force strict boolean values in PHP

Function to return number of digits of an integer in PHP

Function to sort array by elements and count of element in PHP

Floating point precision in PHP

Latest Articles (in PHP)