Re: generically accessing member variables problem

From: Date: Wed, 06 Nov 2002 20:00:29 +0000
Subject: Re: generically accessing member variables problem
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-123071@lists.php.net to get a copy of this message
Ok. I think I got it. Now I have to look at my code again to see if I can implement it. Thanks for your time. jck Maxim Maletsky wrote:
Well, if, say, one class has this function: function inherit($class_name, $pointer) { $this->objects[$class_name] = $pointer; } then you end up knowing that, whenever you need to access an instance for a class, you can use $this->objects['that_class']->that_array. What I picked on, was that you were assigning keys automatically with ($array[] = ...) and that makes it impossible pointing to the right element directly without having to loop the whole array, which is an overkill. The rest of your logic is pretty complex but doable. As long as you plan off well the complete OOD for that. So, my conclusion is: It is OK storing all objects in one single array, but name then the way you can find them later. If you have multiple instances of the same class, use numeric keys but assign them under a certain logic - not automatically, so you can find/use/destroy them at any point. can't give you more info here as that is all I know of it. Check out the CVs tree for ZoomStats - www.zoomstats.org, it is a 100% OOP PHP app and can give you some hints. -- Maxim Maletsky maxim@php.net John Kenyon <jck@croanoke.com> wrote... :
I am not really sure I understand what you are saying here, and I would like to. Let me first say that I think the syntax you came up with earlier will solve my immediate problem, but if I could design this in a better way I'd like to know. Let me give you a few more details: I have a large class that contains multiple instances of various different classes, in turn, each of these instances could contain multiple instances of other classes. A big tree. In order to keep track of the various instances of the classes I use arrays, so each class that contains instances of other classes keeps like classes within an array. Sometimes a class has multiple arrays, each holding instances of a different type of class. Each of these classes is being used to generate html forms and I want to have a way to have the user check a checkbox and remove a particular instance of a class. I want to have one function that I can plop into each class (in some cases it may be possible to have children inherit the function from a parent class) and which will delete the correct instance of the correct class by passing in the name of the array the instance is held in and its index in that array. I hope this provides you with a little more context and if your advice still pertains, please explain it if you would. jck Maxim Maletsky wrote:
You pass it the name of the element, and whatever the data inside. You do not need to add other sub-elements to it automatically, as you would need to be "searching" through the elements later for the right data. Whatever your need is - it's a good idea using arrays, and add other arrays into it. But, it is a bad idea cloning variables and auto-assign array's elements when you know that you will need that specific piece you store alone. -- Maxim Maletsky maxim@php.net John Kenyon <jck@croanoke.com> wrote... :
     
See below:
       
MM> class Example { MM> var $array = array(); MM> function add2array($element_name, $val){ MM> $this->$array[$element_name] = $val; MM> } MM> } MM> $t = new Example(); $t->>add2array('array1',25); $t->>add2array('array2',26); $t->>add2array('array3',"Hello"); MM> echo '<pre>'; MM> print_r($t); MM> echo '</pre>'; MM> Cleaner and more scalable, no? Yes and to fit the original 3 seperate arrays it would be function add2array($element_name, $val){ $this->$array[$element_name][] = $val; }
           
No, because you pass it the name and data. This way every name will become element's name containing the relevant data. Your way just makes it an associative array without a way of directly accessing it. -- Maxim Maletsky maxim@php.net
         
But my problem is that I have several arrays already and I want to be able to act on a specific array depending on the name I pass in. I don't see how your solution solves that issue. jck
       
     
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#123071) next »