By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,345 Members | 2,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,345 IT Pros & Developers. It's quick & easy.

[php5] static and inheritance

P: n/a

How do I use static functions/properties with inheritance? I've found it
very problematic.

class Foo
{
static function A() {/* ??? */::B(); }
static function B() {echo 'Foo';}
};

class Bar extends Foo
{
static function B() {echo 'Bar';}
}

I'd like Bar::A() to respect inheritance and output 'Bar', but I can't
figure out how to make call from base class to static methods of inherited
class.

Is there equivalent of $this for static calls? Or something being reverse
of 'parent'?
--
* html {redirect-to: url(http://browsehappy.pl);}
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
porneL wrote:
Is there equivalent of $this for static calls? Or something being
reverse of 'parent'?


Only when you instantiate a class, the parent-child hierarchy fully exists
and child method have precedence over parent methods.

If you want a workaround, which is more or less flexible, you could do
something like the following:

static function A() {
foreach (get_declared_classes() as $class) {
if (__CLASS__ == get_parent_class($class)) {
call_user_func(array($class,"B"));
break;
}
}
}
JW

Jul 17 '05 #2

P: n/a
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:41**********************@news.euronet.nl...
porneL wrote:
Is there equivalent of $this for static calls? Or something being
reverse of 'parent'?


Only when you instantiate a class, the parent-child hierarchy fully exists
and child method have precedence over parent methods.

If you want a workaround, which is more or less flexible, you could do
something like the following:

static function A() {
foreach (get_declared_classes() as $class) {
if (__CLASS__ == get_parent_class($class)) {
call_user_func(array($class,"B"));
break;
}
}
}


Errr...your code is exactly equivalent to

static function A() {
Bar::B();
}

provided that Bar is the first class that inherits Foo.

Jul 17 '05 #3

P: n/a
Janwillem Borleffs wrote:
If you want a workaround, which is more or less flexible, you could do
something like the following:

static function A() {
foreach (get_declared_classes() as $class) {
if (__CLASS__ == get_parent_class($class)) {
call_user_func(array($class,"B"));
break;
}
}
}


Wouldn't this just pick the first inherriting class it finds? If there are
more than one class inherriting Foo, it's difficult to be sure which class
method would be called.

--
Tommy

Jul 17 '05 #4

P: n/a
Chung Leong wrote:
Errr...your code is exactly equivalent to

static function A() {
Bar::B();
}

provided that Bar is the first class that inherits Foo.


It's just a suggestion for a method which you can put in any class without
having to care about its name.
JW

Jul 17 '05 #5

P: n/a
Tommy Gildseth wrote:
Wouldn't this just pick the first inherriting class it finds? If
there are more than one class inherriting Foo, it's difficult to be
sure which class method would be called.


Of course it will. I don't say that this method is fullproof, only a more or
less universal way to implement the method.

Perhaps a method that verifies whether both class Bar and method Bar::A()
are defined and Bar extends Foo, is better to handle the dependency, but I
will leave it up to the OP to select the best solution for his problem.
JW

Jul 17 '05 #6

P: n/a
static function A() {
foreach (get_declared_classes() as $class) {
if (__CLASS__ == get_parent_class($class)) {
call_user_func(array($class,"B"));
break;
}
}
}


Wrong way. It's emulation of parent::B(). I need "child::B()" or rather
"current::B()".
--
porneL
Jul 17 '05 #7

P: n/a
porneL wrote:
Wrong way. It's emulation of parent::B(). I need "child::B()" or
rather "current::B()".


When that's the case, why does the following code then print "Bar"?

<?

class Foo {
static function A() {
foreach (get_declared_classes() as $class) {
if (__CLASS__ == get_parent_class($class)) {
call_user_func(array($class,"B"));
break;
}
}
}

static function B() {echo 'Foo';}
};

class Bar extends Foo {
static function B() {echo 'Bar';}
}

Bar::A();

?>
JW

Jul 17 '05 #8

P: n/a
On Wed, 22 Dec 2004 15:23:22 +0100, Janwillem Borleffs <jw@jwscripts.com>
wrote:
porneL wrote:
Wrong way. It's emulation of parent::B(). I need "child::B()" or
rather "current::B()".


When that's the case, why does the following code then print "Bar"?


Oh, sorry, at first glance I didn't notice the loop.

It does, but unfortunetely its pragmatic solution only for example in my
post.
It doesn't solve general problem of static methods and inheritance.
If there are other classes extending Foo, it won't work properly.
I haven't found any appropriate language construct, so I assume that is
design problem/limitation of PHP.
Other OO languages don't require classname for static calls, so base class
isn't forced to call itself only.
I think that there is a hack to solve this problem - debug_backtrace()
returns classes and call types...

--
porneL
* html {redirect-to: url(http://browsehappy.com);}
Jul 17 '05 #9

P: n/a
porneL wrote:
On Wed, 22 Dec 2004 15:23:22 +0100, Janwillem Borleffs <jw@jwscripts.com>
wrote:

I haven't found any appropriate language construct, so I assume that is
design problem/limitation of PHP.
Other OO languages don't require classname for static calls, so base class
isn't forced to call itself only.


Maybe the reason you can't do this, is because the language supposedly
doesn't support it. From the PHP manual:
http://no.php.net/manual/en/language.oop5.static.php
"Declaring class members or methods as static, makes them callable from
outside the object context. A member or method declared with static can not
be accessed with a variable that is an instance of the object and cannot be
re-defined in an extending class."
--
Tommy

Jul 17 '05 #10

P: n/a
OK, one possible answer to your problem is using debug_bactrace in the
following form:

class Foo {
public static function getStaticCallingClass(){
$debugArray = debug_backtrace();
$className = $debugArray[2]['class'];
return $className;
}
public static function A(){
$className = self::getStaticCallingClass();
eval($className . "::B();");
}

public static function B(){
echo "Foo";
}
}

class Bar extends Foo {
public static function B(){
echo "Bar";
}
}
The problem of handling the access of inherited static members and
functions is probably a common one in PHP5.

It is a matter of philosophy of the language.

But I do believe there should be a token which easily addresses this in
PHP5.

-------------------------------------
porneL wrote:
How do I use static functions/properties with inheritance? I've found
it
very problematic. class Foo
{
static function A() {/* ??? */::B(); }
static function B() {echo 'Foo';}
}; class Bar extends Foo
{
static function B() {echo 'Bar';}
} I'd like Bar::A() to respect inheritance and output 'Bar', but I
can't
figure out how to make call from base class to static methods of
inherited
class. Is there equivalent of $this for static calls? Or something being
reverse
of 'parent'?


##-----------------------------------------------#
Article posted with Web Developer's USENET Archiv
http://www.1-script.com/forum
no-spam read and post WWW interface to your favorite newsgroup -
comp.lang.php - 21435 messages and counting
##-----------------------------------------------##
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.