Re: Connecting data

From: Date: Fri, 03 Dec 2004 13:21:18 +0000
Subject: Re: Connecting data
References: 1 2 3 4 5 6 7  Groups: php.gtk.general 
Request: Send a blank email to php-gtk-general+get-5299@lists.php.net to get a copy of this message
Hi, Friday, December 3, 2004, 10:37:22 PM, you wrote: GWS> Well... lets say have the following: GWS> class Thing{ GWS> var $stuff; GWS> function Thing(){ GWS> $button = &new GtkButton(); $this->>stuff = 0; $button->>connect("button-press-event", array($this, "Increment")); $this->>stuff = 10; GWS> } GWS> function Increment(){ $this->>stuff++; echo $this->>stuff; GWS> } GWS> } GWS> When the button is clicked I won't get 11, 12, 13.. etc. I'll get 1, 2, 3 GWS> etc.. because it is taking the value of stuff when I assigned the callback, GWS> NOT when the event is called. Obviously this is a simple example. GWS> The only way I have found to get around this is to pass the callback the GWS> value as a reference (and declare it as such in the callback function as GWS> well). This seems rather... silly and defeats the point of class variables. GWS> Its also one hell of a headache. GWS> So while I have a workaround atm.. its rather a kludge. GWS> -- Greg. GWS> ----- Original Message ----- GWS> From: "Steph" <steph@zend.com> GWS> To: "Gregory Wild-Smith" <greg@twilightuniverse.com>; GWS> <php-gtk-general@lists.php.net> GWS> Sent: Friday, December 03, 2004 12:10 PM GWS> Subject: Re: [PHP-GTK] Connecting data PHP GTK seems to do strange things with events and classes. The way I avoid this is to store class variables in the gtk object with $gtk_object->set_data() and $gtk_object->get_data() This seems to survive events. A pseudo example: class test { var $button; function test(){ $this->button = &new GtkButton(); $this->button->set_data('counter',10); $this->button->>connect("button-press-event", array($this, "Increment")); } function Increment(){ $counter = $this->button->get_data('counter'); $counter ++; $this->button->set_data('counter',$counter); } } -- regards, Tom

« previous php.gtk.general (#5299) next »