Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts

    OOP :: Not getting anything returned

    PHP Code:
    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?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Did you ever create an instance of your object before you tried to access the functions in it?

    variablename = new ObjectName();
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your page_title() method, you don't use the parameter string as a key to reach the array element:

    PHP Code:
    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.
    De gustibus non est disputandum.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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>
    Last edited by Nightfire; 09-25-2003 at 04:34 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •