...

View Full Version : OOP :: Not getting anything returned



Nightfire
09-25-2003, 04:04 PM
class pages{

var $titles = array("index" => "Welcome",
"credits" => "Site credits");

function page_title($which){
return $this->titles[];
}
}


echo '<html><head><title>'.$pages->page_title("index").'</title></head></html>';


This is my first attempt at OOP and I have no idea why I'm not getting anything in the title of the page. I've not used functions or arrays for ages, so I'm guessing it's something to do with them too. Any ideas?

Spookster
09-25-2003, 04:10 PM
Did you ever create an instance of your object before you tried to access the functions in it?

variablename = new ObjectName();

mordred
09-25-2003, 04:26 PM
In your page_title() method, you don't use the parameter string as a key to reach the array element:



function page_title($which){
return $this->titles[$which];
}


Spookster is right though that you have to create an instance of the object somewhere. You can't even use a static all in your sample code, because you rely on the class variable $this->titles.

Nightfire
09-25-2003, 04:27 PM
I think I should've read a different tutorial :) THe one I read didn't mention that. All fixed now, ta

<edit>It's not gonna be used for anything this class, I'm just playing with it. After I've played a bit, I'll try and make something more useful and flexible</edit>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum