473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Child class references static variable of parent instead of its own

Here's an OOP question that perplexes me. It seems PHP doesn't treat
static variables correctly in child classes.

<?php
class ABC {
public $regular_variable = "Regular variable in ABC\n";
public static $static_variable = "Static variable in ABC\n";

public function regular_function() {
echo $this->regular_variable;
}

public static function static_function() {
echo self::$static_variable;
}
}

class DEF extends ABC {
public $regular_variable = "Regular variable in DEF\n";
public static $static_variable = "Static variable in DEF\n";
}

$abc = new ABC();
$abc->regular_function();
ABC::static_function();

$def = new DEF();
$def->regular_function();
DEF::static_function();
?>

WHAT I EXPECTED:
Regular variable in ABC
Static variable in ABC
Regular variable in DEF
Static variable in DEF

ACTUAL OUTPUT:
Regular variable in ABC
Static variable in ABC
Regular variable in DEF
Static variable in ABC <--- This is different from what I expected

Anyone know why?

There are several examples of this bug (if it is one) in the comments
for the get_class() function: http://us.php.net/manual/en/function.get-class.php

Best regards,
Richard

Aug 17 '07 #1
3 3240
The manual says that static variables are not inherited normally:

http://nl3.php.net/manual/pl/language.oop5.static.php

Best regards.
Aug 17 '07 #2
On Aug 17, 3:04 pm, Dikkie Dik <dik...@nospam.orgwrote:
The manual says that static variables are not inherited normally:

http://nl3.php.net/manual/pl/language.oop5.static.php

Best regards.
Thanks for pointing that out; I hadn't noticed.

Minutes from a meeting about PHP 6 seem to indicate that this will be
fixed:

http://www.php.net/~derick/meeting-n...different-name
Aug 17 '07 #3
Richard K Miller wrote:
Here's an OOP question that perplexes me. It seems PHP doesn't treat
static variables correctly in child classes.
Google: PHP late static binding.

It *might* get fixed in PHP 6, but is unlikely to change in PHP 5.x
because it may break scripts that rely on the current behaviour.

-Toby
Aug 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
0
by: sshuangw | last post by:
Hello: I am encountering a very weird issue with MDI child, Overriden WndProc function and hidden form. Basically, the application has two forms, Form1(parent form), Form2(Child form),...
5
by: pcPirate | last post by:
Okay, I've have a mdiparentform and only one mdichild form. My parent form has a toolbar with only one button "Open Record". When a user click on the button, a child form would be shown. My...
3
by: Maheshkumar.R | last post by:
Hi groups, How i can command over the MDI CHIlD forms created dynamically at runtime from PARENT. Let say, i have generated 5 mdichild forms, but i want to work with child form1 from MDI...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
6
by: Edwinah63 | last post by:
Hi everyone, could someone give me some thoughts on the best way to manage mdi parent and child forms? in vb6 i could scroll through the forms collection and determine which forms were...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
11
by: manstey | last post by:
Hi, I am having trouble designing my classes. I have two classes. The first one wraps around an old-style class called oref Class CacheClass(object): def __init__(self, obj):
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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...

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.