Detect Calling Class?

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
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Detect Calling Class?

Post by Todd_Z »

I want to detect what class is calling a static function in another class... is that possible?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

I bow to your H4X
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Continuation of that....

Is there anyway to call a certain function before a function of a class is called?

For example:

Code: Select all

function cool ( ) {
  return $cashMoney > 300;
}

function isFeyd ( ) {
  if ( $_SESSION['user'] == "Feyd" )
    return true;
}
basically, I want to call function cool, and it would call isFeyd() before starting the main body of the function.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

outside of the runkit extension and PHP 5, I don't believe so.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Todd_Z wrote:I want to detect what class is calling a static function in another class... is that possible?
That sounds like you are trying to do something that should probably be handled in a better way.
Todd_Z wrote:Is there anyway to call a certain function before a function of a class is called?
In PHP5 you could use __call() to intercept the method calls and run any code you wanted before running the requested code.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

arborint wrote:
Todd_Z wrote:I want to detect what class is calling a static function in another class... is that possible?
That sounds like you are trying to do something that should probably be handled in a better way.
Todd_Z wrote:Is there anyway to call a certain function before a function of a class is called?
In PHP5 you could use __call() to intercept the method calls and run any code you wanted before running the requested code.
Bear in mind that if the method you're calling a method which actually exists __call() will be ignored so write a routine that makes internal calls to private methods with systematically similiar names. i.e:

Code: Select all

private function _getFoo()
{
    //
}
Where __call() executes the method from inside the class if simply getFoo() is called ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

d11wtq wrote:Bear in mind that if the method you're calling a method which actually exists __call() will be ignored so write a routine that makes internal calls to private methods with systematically similiar names.
Yes. Unfortunately __call(), __get() and __set() are error handlers and not real runtime overloading. But you can fake if you control the naming.
(#10850)
Post Reply