473,386 Members | 2,114 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,386 software developers and data experts.

access child static property

bilibytes
128 100+
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:

Expand|Select|Wrap|Line Numbers
  1. protected static $_multiLevelArray = array('one'=> array(...), 'two'=> array(...))
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. protected static setChildStaticProperty($key){
  2.      child::$_finalArray = child::$_multiLevelArray[$key];
  3. }
is this "child::" valid in php5?

or i should have the same method replicated on each extending class?

any suggestion?

cheers
Jun 1 '09 #1
12 4952
Dormilich
8,658 Expert Mod 8TB
@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.
Jun 1 '09 #2
bilibytes
128 100+
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.
Jun 1 '09 #3
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class cParent {
  3.     public static $error;
  4.  
  5.     public static function setVariable($key, $owner=__CLASS__) {
  6.         try {
  7.             $refClass = new ReflectionClass($owner);
  8.             $childStorage = $refClass->getStaticPropertyValue("storage");
  9.             $refClass->setStaticPropertyValue("final", $childStorage[$key]);
  10.         }
  11.         catch(ReflectionException $ex) {
  12.             self::$error = $ex->getMessage();
  13.         }
  14.     }
  15. }
  16.  
  17. class cChild extends cParent {
  18.     public static $storage = array("First", "Second", "Third");
  19.     public static $final;
  20.  
  21.     // This override method is not needed, but makes
  22.     // this functionality easier to use.
  23.     // Downside is that you will have to duplicate it in every child.
  24.     public static function setVariable($key, $owner=__CLASS__) {
  25.         parent::setVariable($key, $owner);
  26.     }
  27. }
  28.  
  29. // By adding the setVariable override
  30. // to the child class you can do this.
  31. cChild::setVariable(2);
  32. echo cChild::$final; // Prints: Three
  33.  
  34. // If you leave it out you would have to do:
  35. cParent::setVariable(0, "cChild");
  36. echo cChild::$final; // Prints: One
  37. ?>
Would be much simpler to just copy/paste a simple method into all the child classes:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class cParent {
  3.     protected static $storage = array("First", "Second", "Third");
  4.     public static $final;
  5.     public static function setVariable($key) {
  6.         self::$final = self::$storage[$key];
  7.     }
  8. }
  9.  
  10. class cChild extends cParent {
  11.     protected static $storage = array("Child's First", "Child's Second", "Child's Third");
  12.     public static $final;
  13.     public static function setVariable($key) {
  14.         self::$final = self::$storage[$key];
  15.     }
  16. }
  17.  
  18. class cGrandChild extends cParent {
  19.     protected static $storage = array("Fourth", "Fifth", "Sixth");
  20.     public static $final;
  21.     public static function setVariable($key) {
  22.         self::$final = self::$storage[$key];
  23.     }
  24. }
  25.  
  26. cChild::setVariable(2);
  27. echo cChild::$final;
  28.  
  29. cGrandChild::setVariable(2);
  30. echo cGrandChild::$final;
  31. ?>
Jun 1 '09 #4
bilibytes
128 100+
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.
Jun 1 '09 #5
bilibytes
128 100+
@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:

Expand|Select|Wrap|Line Numbers
  1. protected static $_multiLevelArray = array('one'=> array(...), 'two'=> array(...))
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. protected static setChildStaticProperty($key){
  2.      child::$_finalArray = child::$_multiLevelArray[$key];
  3. }
instead of this:
Expand|Select|Wrap|Line Numbers
  1. protected static setChildStaticProperty($key){
  2.      child::$_finalArray = child::$_multiLevelArray[$key];
  3. }
you would access it like this:
Expand|Select|Wrap|Line Numbers
  1. protected static setChildStaticProperty($key){
  2.     static::$_finalArray = static::$_multiLevelArray[$key];
  3. }
I'm impatient to try it!

cheers.
Jun 10 '09 #6
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 :)
Jun 10 '09 #7
Markus
6,050 Expert 4TB
@Atli
Now I just gotta wrestle with Windows so it plays nice and upgrades smoothly -.-
Jun 10 '09 #8
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 :)
Jun 10 '09 #9
Markus
6,050 Expert 4TB
@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.
Jun 10 '09 #10
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.
Jun 10 '09 #11
Markus
6,050 Expert 4TB
@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.
Jun 10 '09 #12
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
Jun 10 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
3
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...
4
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...
1
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...
2
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?...
4
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() {...
10
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...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
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
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
marktang
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,...
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...

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.