Jump to content

Recommended Posts

From a static method, is it possible to definitively get the name of the class it is *running* in,

even if the method was inherited?  I know the self keyword and __CLASS__ constant are both bound at

compile-time to the class the method was *defined* in, which is not what I want.  As well, calling

the built-in get_class() without an argument also uses the compile-time bound class.

 

Take the following code example:

 

class parent_class {
public  function foo() {
	// do stuff...

	// attempting to call the bar method of the run-time bound class...
	call_user_func(
		array( get_class(), 'bar' )
	);
}
// this is the compile-time bound class
protected static function bar() {
	// do something...
	echo "parent!";
}
}

class child_class extends parent_class {
// this is the run-time bound class
protected static function bar() {
	// do something ELSE...
	echo "child!";
}
}

// invoke parent_class::bar()
parent_class::foo();

// ideally, invoke child_class::bar()
child_class::foo();

 

I can't simply bypass foo, because it does a significant amount of stuff *before* calling bar.  Is there anything I can do without changing the entire structure of the class(es) from static to instantiated?

You've given me a headache.

 

Whenever people have questions like this (and yes, it happens often enough) it usually results in a fatal flaw in design and indicative of poor forethought.

 

Why can't you do the following?

 

<?php
class parent_class {
public  function foo() {
	// do stuff...

	// attempting to call the bar method of the run-time bound class...
	call_user_func(
		array( get_class(), 'bar' )
	);
}
// this is the compile-time bound class
protected static function bar() {
	// do something...
	echo "parent!";
}
}

class child_class extends parent_class {
// this is the run-time bound class
protected static function bar() {
	// do something ELSE...
                self::foo(); // added.
	echo "child!";
}
}

// invoke parent_class::bar()
parent_class::foo();

// ideally, invoke child_class::bar()
child_class::foo();
?>

 

Or am I missing the point completely?

First, I should note that I mistyped in the original code example...The method parent_class::foo should be static.

 

public static function foo() {
// do stuff...

// attempting to call the bar method of the run-time bound class...
call_user_func(
	array( get_class(), 'bar' )
);
}

 

As for your suggestion, keeB, I don't really understand what you mean.  The entry point into the class is the public static method foo, which does some preprocessing, then calls the protected static method bar.  What I want to do, is call the child_class::bar method within foo instead of parent_class::bar.  Your change is in the child bar method that never gets called.

 

I may be completely mis-communicating what I want to do, and if so I apologize.

You cannot do that. Late static binding is not supported yet. It's coming in PHP 5.3.0.

 

http://php.net/manual/en/language.oop5.late-static-bindings.php

 

Edit: It seems there is a comment on the bottom suggesting that you can use get_class() to get around that. You might want to check that out.

My particular use is admittedly an edge case, where I'm extending an entirely static class (all static methods and properties).  In truth, I'm using it more as a workaround for the lack of namespaces.

 

The workaround using get_class only works if you have an instance of the object, which I don't in this case because I'm using all static functions.  So, as that example suggests, I'm going to turn what was a completely static class into a sort of singleton, where I'll have an instance that I can use to get the run-time class.

 

As far as using Reflection, I looked into that as well.  It faces the same problem as get_class, where you need to specify an instance.

 

Thanks, all.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.