Tran Dinh Trong's Blog

Stuydy and share

parent:: AND self::

PHP supports two reserved class names that make it easier when writing OO
applications. 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.

  • 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.


class Ancestor {
const NAME = "Tran Dinh Trong";
function __construct() {
print "In ". self::NAME." Construct";
}
}
class Child extends Ancestor{
const NAME = "Tran Dinh Thien";
function __construct() {
parent::__construct();
print "In ".self::NAME." Construct";
}
}
$child = new Child();

In Tran Dinh Trong Construct

In Tran Dinh Thien Construct

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: