Re: Object and reference problem
| From: | SafeV | Date: | Thu, 18 Oct 2001 12:55:04 +0000 |
| Subject: | Re: Object and reference problem | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-71477@lists.php.net to get a copy of this message | ||
Sigh, I guess you're right...
But I think a better solution is to wrap strings in a String object
Thanks for your input!
Brian White wrote:
I think you are going to have to live with it. Functional overloading only really makes sense in strongly typed languages, and I bet is a bugger to implement in a name based scripting language so I don't think you are goign to get it. What I have sometimes wished for was that all objects and arrays were passed around as pointers by default, with the ability to create a copy when required, but that is such a fundamental change that I doubt if it will happen, and there are probably goo reasons not to ( have I ever told people how much I like Python ... ) Maybe you are just going to have to bite the bullet and have an "AddRef" method...... At 04:28 17/10/2001 +0200, SafeV wrote:Someone, have a go at this! I have encountered a very troublesome problem, which I don't see how to solve in an elegant way, since PHP doesn't support method overloading. Suppose my class is, kind of a tree, like: class A { var $children; function A() {------------------------- Brian White Step Two Designs Pty Ltd - SGML, XML & HTML Consultancy Phone: +612-93197901 Web: http://www.steptwo.com.au/ Email: bwhite@steptwo.com.au$this->children = array();} // I want this to accept both objects and strings etc. // IE. mixed function add($child) {$this->children[] = $child;} function printStuff() {foreach ($this->children as $child) { if (is_object($child)) $child->printStuff(); else echo $child; }} } Now this is fine: 1 $obj1 = new A(); 2 $obj2 = new A(); 3 $obj1->add("Something"); 4 $obj2->add(", something else"); 5 $obj1->add($obj2); 6 $obj1->printStuff(); and prints out "something, something else" But if I swap lines 4 and 5 it only prints out "something". Obviously, the add method of $obj1 only receives a copy of $obj2, and if I try to add something to $obj2 afterwards, even thought it's supposed to be hooked with $obj1, it will "disappear". For objects only I could solve this with declaring add() like: function add(&$child) {$this->children[] = $child;} So it receives a reference of the object, but then I can't add a string (fatal error, cannot pass string as reference), or an integer or double, for that matter! And I really don't want to use different method names! I don't have full control over the reference stuff in PHP, does anyone know how I can fix this? Or a workaround? Thanx! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net