Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Location
    PA
    Posts
    303

    Question Switch Function Problem

    Im having problems with this switch function. When it goes to default i get this:

    Fatal error: Call to a member function on a non-object in /home/user/public_html/test.phtml on line 32
    Below is the source code:

    PHP Code:
    <?php

    require ('functions.php');
    $id $_GET['page'];
    $site = new Page;
    switch (
    $id){

    case 
    'about_us':
    $this->about_us();
    break;

    default:
    $this->home();
    break;


    function 
    home()
    {
    global 
    $site;
    $site -> head();
    $site -> body();
    $site -> loginform();
    $site -> topmenu();
    $site -> flashheader();
    $site -> mainplans();
    $site -> copyrights();
    $site -> footer();
    }
    }


    exit;
    ?>

  2. #2
    Put the function before the switch statement.

  3. #3
    Join Date
    Aug 2003
    Location
    PA
    Posts
    303
    nope that didnt work. i get the same messege

  4. #4
    Code:
    <?php 
    
    require ('functions.php'); 
    $id = $_GET['page']; 
    $site = new Page; 
    
    function home() 
    { 
    global $site; 
    $site -> head(); 
    $site -> body(); 
    $site -> loginform(); 
    $site -> topmenu(); 
    $site -> flashheader(); 
    $site -> mainplans(); 
    $site -> copyrights(); 
    $site -> footer(); 
    }
    
    switch ($id){ 
    
    case 'about_us': 
    $this->about_us(); 
    break; 
    
    default: 
    $this->home(); 
    break;  
    } 
    ?>
    Like this?

  5. #5
    Join Date
    Apr 2004
    Location
    Port St Lucie, FL
    Posts
    117
    First, I'd try to take the function out of the switch statement, and place it before the switch.

    Second, do you really need the $this-> call? If you want to call the about_us method on the Page class, then you need to use $site instead of $this. Otherwise, you can just call about_us() without the $this. Same goes for home().

    Good luck..
    Paul Embry
    Knight Software and Web Design
    Paul.Embry@gmail.com
    Quality PHP Web Programming for Reasonable Prices

  6. #6
    Join Date
    Aug 2003
    Location
    PA
    Posts
    303
    Originally posted by nathank
    Code:
    <?php 
    
    require ('functions.php'); 
    $id = $_GET['page']; 
    $site = new Page; 
    
    function home() 
    { 
    global $site; 
    $site -> head(); 
    $site -> body(); 
    $site -> loginform(); 
    $site -> topmenu(); 
    $site -> flashheader(); 
    $site -> mainplans(); 
    $site -> copyrights(); 
    $site -> footer(); 
    }
    
    switch ($id){ 
    
    case 'about_us': 
    $this->about_us(); 
    break; 
    
    default: 
    $this->home(); 
    break;  
    } 
    ?>
    Like this?
    Yeah that didnt work. But stormraven advice works. Its always something simple. Thanks all for the advice.

  7. #7
    Join Date
    May 2004
    Location
    Singapore
    Posts
    263
    As mentioned:
    1. Place the function outside the switch selection block.
    2. Since you're not defining home() as a member function of a class, dont use it as such.

    PHP Code:
    <?php
    require 'functions.php';

    function 
    home(&$site)
    {
        
    $site->head();
        
    $site->body();
        
    $site->loginform();
        
    $site->topmenu();
        
    $site->flashheader();
        
    $site->mainplans();
        
    $site->copyrights();
        
    $site->footer();
    }

    $site = &new Page;

    if (!empty(
    $_GET['page'])) {
        switch (
    $_GET['page']) {
        case 
    'about_us':
            
    $site->about_us();
            break;
        default:
            
    home($site);
        }
    }

    exit;
    ?>
    #include<cstdio>
    char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
    int main(){std::printf(s,10,34,s,34,10);}

Posting Permissions

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