473,466 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[php5] static and inheritance


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
10 2480
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
"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten B. | last post by:
Hi, I need to add a string to a class var, but it doesn´t work though I read it is a new feature in PHP5. This is my code: class Huh{ static $message; public static function...
2
by: Chung Leong | last post by:
Say I have a class in PHP5 that looks like this: class Hello{ function Moo() { } static function Boo() { }
4
by: Daniel Andersen | last post by:
Hey, We are in the process of rewriting our internal management system (which was written in PHP4), and figured this would be a good time to migrate to PHP5 to get all the OO goodness it has to...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
0
by: Tim Meader | last post by:
Hello, I was hoping someone could provide some insight as to the best approach to take in porting over a function I use in quite a few spots from PHP4 to PHP5. Basically, I have a db wrapper class...
2
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
30
by: Logos | last post by:
I have what may be a bug, or may be a misunderstanding on how pass by reference and class inheritance works in PHP. Since I'm relatively new to PHP, I'm hoping for a little outside help to shed...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.