Re: Why doesn't this work? Driving me crazzzzzy

From: Date: Thu, 07 Jul 2005 01:31:21 +0000
Subject: Re: Why doesn't this work? Driving me crazzzzzy
References: 1  Groups: php.gtk.general 
Request: Send a blank email to php-gtk-general+get-5916@lists.php.net to get a copy of this message
On Wed, July 6, 2005 10:25 am, Jake Gordon said: > I've never really used much OO programming before, so I expect the > answer to this is really easy, but I've lost most my hair now having > been trying to figure out where the hell I'm going wrong for the last 3 > hours. > > The code is meant to change the contents of a part of the layout by > destroying the object $main if it already exists, and then creating a > new object with the same name and putting it in the same place. > > I call it using: > $button->connect('clicked', 'change_main', $section); > > The function is: > > function change_main($button, $section, $main) { > global $layout, $main; Your problem is that you've got *TWO* different things, both of them trying to be named "main" You've got that thing coming in, as an argument, called $main. Then, you use global $main; to make the old thing, called $main outside the function, also be called $main *INSIDE* the function. So when you first call the function whatever is passed in is called $main ; But as soon as you do that global $main, then the "old" main outside the function is called $main. Now, if they are *BOTH* really the same thing, then you don't need the global $main at all. If they're supposed to be different, change your function definition to be something like: function change_main($button, $section, $something_else){ > // First destroy what was here before, if anything > if($main) $main->destroy(); > > $main = &new GtkLabel($section); > $layout->put($main,100,40); > $main->show(); > } > > If I call the function like this instead: > $main = change_main(null, "splash", $main); > ... with 'return $main' at the bottom of the function, it works. But how > can I do this without returning $main seeing as ->connect doesn't allow > this, no? Ah. Maybe you can ignore what I said above, mostly. Since you have global $main; in your function, you don't need to return $main to assign it to $main. You can just do: change_main(null, "splash", $main); And when you do $main = &new GtkLabel($section); you *ARE* changing $main. So you don't need to return it, because you don't need to do the assignment outside the function. You've already changed $main by the time the function returns. I'm not sure it's a Good Idea to do it this way, mind you, but it should "work" -- Like Music? http://l-i-e.com/artists.htm

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