How to know the name of the called function in php

I have just started migrating from procedural php to object oriented php. Since object oriented is easier to maintain and is more organized that procedural. First thing that I want to tackle about object oriented php is determining the name of the function that you are calling.

Here’s a sample class and a sample function. I assigned 1 parameter with the name $caller and assigned it to be equal to __FUNCTION__ which is a magic constant in php. Then I just echo back the variable $caller.

class sampleclass{
     function mysqlselectall($caller=__FUNCTION__){
        echo $caller. ": ";
        $this->mysqlFetchAssoc("SELECT * FROM testtable");       

So that every time you try to call that function mysqlselectall it will first echo its name then try to perform the actions that you supplied.



I can’t really think of how this would be useful. But maybe this can help in debugging purposes. Especially when you think that you are calling a certain function but its actually not the function that is being executed.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s