Is there a way to get name of class which calls a method?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Is there a way to get name of class which calls a method?

Post by mcccy005 »

Is there a way to get the name of a particular class/object/file which has called a particular method from within the method itself??

Basically I want only a particular class to be able to call a particular method of another class without extending from one to the other etc.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I think your best bet would be to send it as a method argument:

Code: Select all

$someClass::methodName('arg1',__CLASS__);
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: Is there a way to get name of class which calls a method

Post by stereofrog »

mcccy005 wrote:Is there a way to get the name of a particular class/object/file which has called a particular method from within the method itself??

Basically I want only a particular class to be able to call a particular method of another class without extending from one to the other etc.
You can use debug_backtrace, but the whole thing seems quite odd to me. Care to explain why you think you need this?
mcccy005
Forum Contributor
Posts: 123
Joined: Sun May 28, 2006 7:08 pm

Post by mcccy005 »

Main reason is that the method I'm using allows certain fields and corresponding values to be deleted from a generated sql query. I only want access to this particular method from one particular class.
Thats primarily why.

I did read something about that backtrace in other posts whilst searching but everyone said it was a bit dodgy so figured I'd try my luck with this post.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply