PHP

How to get the child (or child-child) class name inside of a parent

Partially taken from this Stack Overflow question.

You have three classes:- MongoDB.php which is the parent of Model.php which is the parent of Session.php. What would be an easy way of getting Session (class name) in MongoDB?

function getCollectionClass(){
    return get_class($this);
}

get_class() means get the instance name of the procedure class whilst get_class($this) means get the instance name of the class in $this context and since $this is actually the Session.php class it will return Session.

Advertisements

One thought on “How to get the child (or child-child) class name inside of a parent

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