Results 1 to 2 of 2

Thread: call to non-member function - need help!!

  1. #1
    Join Date
    May 2009
    Posts
    3

    call to non-member function - need help!!

    Okay so I am pretty new to OOP in PHP (used to php4) and I understand that you need some $this in front of other stuff, however I don't use any functions in my function, yet it still doesn't work. Here's the code:

    Library function:
    PHP Code:
    function navBar($thisLink,$thatLink,$nav = array()) {
            
    $CI =& get_instance();
            
            
    $string '';
            
    $baseUrl 'http://rpg.antiradiant.com/';
        
            if (
    $thisLink != $thatLink) {
                
    $string .= '<a href="'.$thatLink.'" class="navAnchor"><img width="170" height="35" src="'.$baseUrl.'media/images/UI/nav/profile/off.jpg" srcover="'.$baseUrl.'media/images/UI/nav/profile/hov.jpg" srcdown="'.$baseUrl.'media/images/UI/nav/profile/on.jpg"/></a>';
            } else {
                
    $string .= '<table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td><a href="'
    .$thisLink.'" class="navAnchor"><img width="170" height="30" src="'.$baseUrl.'media/images/UI/nav/hangar/slct_top.jpg"/></a></td>
                    </tr>'
    ;
                if (
    count($nav) > 0) {
                    
    $string .= '<tr>
                        <td class="navMore">
                        <table border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="30">&nbsp;</td>'
    ;
                    foreach (
    $nav as $row) {
                        
    $string .= '<td><a href="'.$row['anchor'].'">'.$row['link'].'</a></td>';
                    }
                    
    $string .= '<td width="30">&nbsp;</td>
                            </tr>
                        </table></td>
                    </tr>'
    ;
                }
                    
                
    $string .= '<tr>
                        <td><img width="170" height="10" src="'
    .$baseUrl.'media/images/UI/nav/slct_bot.jpg"/></td>
                    </tr>
                    </table>'
    ;
            };
            
            return 
    $string;
        } 
    controller function:
    PHP Code:
    function index()
        {
            
    $this->load->view('test');
        } 
    view file:
    PHP Code:
    $navInfo = array(
        
    => array(
            
    'anchor' => 'profile',
            
    'link'   => 'Profile' 
        
    ),
        
    => array(
            
    'anchor' => 'alliance',
            
    'link'   => 'Alliance' 
        
    )
    );

    echo 
    $this->ui->navBar('profile','profile',$navInfo); // THIS IS LINE 13 
    BTW, this is built in the framwork CodeIgniter and they were of no help what so ever. The error is:

    Fatal error: Call to a member function navBar() on a non-object in /nfs/c02/h07/mnt/XXXXX/domains/aPlace.antiradiant.com/html/system/application/views/test.php on line 13

  2. #2
    Join Date
    Feb 2007
    Location
    Ireland
    Posts
    1,007
    Ok just a few basics.

    A class is a blueprint of an object. It defines the attributes and methods of an object. An instance of a class is when you take that class and build an object from it.

    e.g.

    PHP Code:
    $alan = new Person(); 
    The $this variable is a reference to the object you are working with. Because it references an object, you need to instanciate the class first. The "->" operator is an identifier that links an object to it's classes' properties.

    e.g.

    PHP Code:
    $alan->name "Alan";

    if(
    $alan->is_cool())
       echo 
    "Alan is a very cool guy!";

    class 
    Person
    {
        public 
    $name;
        public 
    $cool;

        
    // Other code in here. This is only an example

       
    public function is_cool()
       {
          if(
    $this->cool)
             return 
    true;
          else
          {
             
    $this->cool true;
             
    $this->is_cool();
          }
       }

    Now that that is out of the way, I believe your problem is line 13 (as you said).

    Code:
    echo $this->ui->navBar('profile','profile',$navInfo); // THIS IS LINE 13
    So in this line you are saying that the current object ($this), has an object attribute called $ui, which has a function in it's class called navBar.

    I'm not really sure what your class names are so, ill just give you another example.

    PHP Code:
    class Page
    {
       private 
    $database// Stores a database object

       
    public $title;
       public 
    $content;

       public class 
    __construct($title)
       {
          global 
    $database;
          
    $this->database $database;

          
    $this->title $title;

          
    $this->get_content(); // Get the page content from the database
       
    }

       private function 
    get_content()
       {
          
    $title $this->database->make_safe($title);

          
    $query "Select content From pages Where title='$title'";
          if(
    $result $this->database->query($query))
          {
               
    $result->fetch_assoc();
               
    $this->content $result['content'];
          }
          else
              return 
    false;
       }
    }

    // Global Scope

    $database = new Database($host,$user,$pass,$name);

    $mypage = new Page('Home'); 
    I'm probably going into too much depth here, but as you can see the database object is stored in the page class. The $this variable references the class in the current scope.

    I'm not sure of your class names and such but I think the proper line of code is this:

    PHP Code:
    echo $this->navBar('profile','profile',$navInfo); // THIS IS LINE 13 
    If you have any questions, ask away.
    “The best thing about a boolean is even if you are wrong, you are only off by a bit.”

Similar Threads

  1. member section and non member profile viewing
    By cnsellon in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 23 Sep 2008, 01:51 PM
  2. animation function not workin.. please advise
    By JasonChan in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 0
    Last Post: 19 Feb 2007, 11:23 PM
  3. Fatal error: Call to undefined function mysql_connect()
    By sofaspud in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 01 Jan 2006, 11:37 AM
  4. correct use of a function?
    By numbenator in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 24 Sep 2005, 09:51 AM

Tags for this Thread

Posting Permissions

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