hi,
i need some help here.
how can i access a static property of a subclass.
i have an abstract class which will be extended... each extending class will have a static property that will store an array like this: - protected static $_multiLevelArray = array('one'=> array(...), 'two'=> array(...))
-
protected static $_finalArray;
each class will have these two properties. But each key of the $_multiLevelArray will store diferent arrays (where i have array(...))
that is why i cannot simpy store the array in the abstract class.
however i would like to be able to set something like this from the abstract class: - protected static setChildStaticProperty($key){
-
child::$_finalArray = child::$_multiLevelArray[$key];
-
}
is this "child::" valid in php5?
or i should have the same method replicated on each extending class?
any suggestion?
cheers
12 4952 @bilibytes
no. @bilibytes
I think that approach does not make sense in terms of OOP (why should a parent class handle actions, that only occur in the child class?). both properties are not even known to the parent class.
It was to avoid method duplication, but if it's no good practice...
thanks for your reply. I will create a method in each subclass.
Atli 5,058
Expert 4TB
This is a tricky question, because static methods and members aren't inherited by child classes. They simply forward calls to them back to their parents.
And, like you say, there is nothing like the "child" keyword that can be used. (Not that that would be a good solution to this)
So to create a static method in a parent class that could be used by child classes to edit the members of the child class isn't really a practical possibility (At least not in a way I can see).
Any calls to the "inherited" static method on the child class would simply be forwarded back to the parent, where it would edit the parent's members.
But...
For the sake of it; there is a way to do it.
You are not going to want to use it, but it does exists.
Using reflection, you can manipulate static members and methods by their name. (Which will be made obsolete in 5.3, when they allow static members to be referenced by their class names)
And he Magic Constants give us a way to dynamically detect a class name from within it's methods.
So, combining those two, you can do: - <?php
-
class cParent {
-
public static $error;
-
-
public static function setVariable($key, $owner=__CLASS__) {
-
try {
-
$refClass = new ReflectionClass($owner);
-
$childStorage = $refClass->getStaticPropertyValue("storage");
-
$refClass->setStaticPropertyValue("final", $childStorage[$key]);
-
}
-
catch(ReflectionException $ex) {
-
self::$error = $ex->getMessage();
-
}
-
}
-
}
-
-
class cChild extends cParent {
-
public static $storage = array("First", "Second", "Third");
-
public static $final;
-
-
// This override method is not needed, but makes
-
// this functionality easier to use.
-
// Downside is that you will have to duplicate it in every child.
-
public static function setVariable($key, $owner=__CLASS__) {
-
parent::setVariable($key, $owner);
-
}
-
}
-
-
// By adding the setVariable override
-
// to the child class you can do this.
-
cChild::setVariable(2);
-
echo cChild::$final; // Prints: Three
-
-
// If you leave it out you would have to do:
-
cParent::setVariable(0, "cChild");
-
echo cChild::$final; // Prints: One
-
?>
Would be much simpler to just copy/paste a simple method into all the child classes: - <?php
-
class cParent {
-
protected static $storage = array("First", "Second", "Third");
-
public static $final;
-
public static function setVariable($key) {
-
self::$final = self::$storage[$key];
-
}
-
}
-
-
class cChild extends cParent {
-
protected static $storage = array("Child's First", "Child's Second", "Child's Third");
-
public static $final;
-
public static function setVariable($key) {
-
self::$final = self::$storage[$key];
-
}
-
}
-
-
class cGrandChild extends cParent {
-
protected static $storage = array("Fourth", "Fifth", "Sixth");
-
public static $final;
-
public static function setVariable($key) {
-
self::$final = self::$storage[$key];
-
}
-
}
-
-
cChild::setVariable(2);
-
echo cChild::$final;
-
-
cGrandChild::setVariable(2);
-
echo cGrandChild::$final;
-
?>
very good answer! I appreciate.
My design is in the border of many solutions but none of them is "very" satisfying.
Because i have:
-a) many subclasses
-b) many instances per subclass
-c) a method with a big body
which means that the solutions which would consist of:
-1) duplicating the static method in each subclass is not a good solution ----->
a)because there are many subclasses.
-2) not setting the properties as static to allow the parent to edit its child properties is not a good solution ----> b)because there are many instances per subclass, and it would imply a lot of duplicated data
between the two options, I would choose 1) because i have less subclasses than the sum of all subclasses' instances. The properties will not be duplicated. However b) the method is quite big so i should consider the difference of bytes between the sum of duplicated properties and the sum of duplicated methods.
but i won't do that calculation!!! lol
i'll stick to 1)
thank you very much
i learnt a bit of "reflection" in the way.
@Dormilich
I discovered last week that PHP 6 has a solution to my problem.
They call it "Late Static Binding" and instead of "child" as i mentioned in my first post, you access the descendant static members with the word " static".
Based on my example:
i have an abstract class which will be extended...
each extending class will have a static property that will store an array like this: - protected static $_multiLevelArray = array('one'=> array(...), 'two'=> array(...))
-
protected static $_finalArray;
each class will have these two properties. But each key of the $_multiLevelArray will store diferent arrays (where i have array(...))
that is why i cannot simpy store the array in the abstract class.
however i would like to be able to set something like this from the abstract class: - protected static setChildStaticProperty($key){
-
child::$_finalArray = child::$_multiLevelArray[$key];
-
}
instead of this: - protected static setChildStaticProperty($key){
-
child::$_finalArray = child::$_multiLevelArray[$key];
-
}
you would access it like this: - protected static setChildStaticProperty($key){
-
static::$_finalArray = static::$_multiLevelArray[$key];
-
}
I'm impatient to try it!
cheers.
Atli 5,058
Expert 4TB
Nice. I'm going to have to look into that. Haven't really gone over the new features in PHP6. (Only just started thinking about PHP5.3)
You can try it out tho.
They release Windows Binaries of the source snapshots for all the PHP versions under development. (6.0, 5.3 and 5.2)
Expect them to be buggy tho :)
@Atli
Now I just gotta wrestle with Windows so it plays nice and upgrades smoothly -.-
Atli 5,058
Expert 4TB
If you get Windows to play nice, please let me know.
I always prefer knowing about impending apocalypses before hand :)
@Atli
I tried for 15 minutes... nada. Apache keeps complaining of not finding a module, blah blah. I wish I could get Ubuntu connected to the net on this damn computer; it'd make things much, much easier.
Atli 5,058
Expert 4TB
Yea, I couldn't get it to work either. Not in the mood to troubleshoot a snapshot version :P
I'll try again tomorrow. Maybe somebody will have fixed whatever is causing it by then. (I would try, but I wouldn't even know where to start.)
Ubuntu doesn't connect to the net on your PC?
That's odd... Graphics problems I'd expect, but not network issues.
@Atli
I'll give it (PHP 6) another shot later. I suppose I could just run it from the command line; that would not require little configuration, I guess.
Everything works when I install Linux, apart from the internet. I think I tried for maybe 5+ hours trying to get it to run, constantly rebooting into Vista to find extra information and then back into Ubuntu to apply all the small rays of hope I had found. All the no avail though. I'm installing Ubuntu again, actually, as we speak. I fancy giving it another shot.
Atli 5,058
Expert 4TB
You could always just use VirtualBox.
Very good for testing out this sort of stuff.
I'm actually compiling PHP6 on Ubuntu via Windows7+VirtualBox atm :P
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Amit |
last post by:
I'm supporting an asp dotnet application. There is a static property
in the code as below. Can anyone tell me in multiuser enviornment,
this propery will return a different dataset each time it is...
|
by: Erik Harris |
last post by:
I apologize if this is a stupid question - I'm relatively new to OOP.
I have a property that must exist in a class in order to be used by another class. The property, however, does not change with...
|
by: Steve |
last post by:
I'm currently running into a problem, and I have no idea what to make of it.
I have a class with nested clases and static properties that I'm using
to store configuration information using a...
|
by: romeo_a_casiple |
last post by:
I'm looking to create an static object that has application scope and
be able to access that static object from my web service.
I understand there are two ways to do this :
1) use the object...
|
by: victor |
last post by:
Hi,
Is it possible to create a Button control as a static property, via
the Form Designer, so that the instantiating code in the wizard
generated 'InitializeComponent()' need not to be modified?...
|
by: Ole Nielsby |
last post by:
Here comes a generic class with a static property.
Let's say they are exotic singleton pets.
abstract class Pet {...}
abstract class SharedPet<T>: Pet
where T: SharedPet<T>, new()
{...
|
by: Franky |
last post by:
I think I misread a post and understood that if I do:
System.Windows.Forms.Cursor.Current = Cursors.WaitCursor
there is no need to reset the cursor to Default.
So I made all the reset...
|
by: dolphin |
last post by:
Hello everyone! Can a static member function access non-static member?
I think it is illegal.Is it right?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
| |