CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2007
    Posts
    13

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Hello friends,

    I have problems with this:

    MenuItem.class.php:
    Code:
    <?php
    	class MenuItem {
    		
    		var $label;
    		var $href;
    		var $target;
    		
    		function MenuItem($label="", $href="", $target="") {
    			$this->label = $label;
    			$this->href = $href;
    			$this->target = $target;
    		}
    		
    		function isEmpty() {
    			return $this->label == "" && $this->href == "" && $this->target == "";
    		}
    		
    		function toString() {
    			return "<td><a href='".$this->href."' target='".$this->target."'>".$this->label."</a></td>";
    		}
    		
    	}
    ?>
    MenuBar.class.php:
    Code:
    <?php
    include_once("MenuItem.class.php");
    
    class MenuBar {
    	
    	var $items;
    	
    	function MenuBar() {
    		$this->items = array(new MenuItem());
    	}
    	
    	function add($item) {
    		if ($this->items[0]->isEmpty()) {
    			$this->items[0] = $item;
    		} else {
    			$this->items[count($this->items)] = $item;
    		}
    	}
    	
    	function show() {
    		$me = "<table><tr>";
    		$j = count($this->items);
    		for ($i=0; $i<$j; $i++) {
    			$me .= $this->items[$i]->toString();
    		}
    		$me .= "</tr></table>";
    	}
    	
    }
    ?>
    menu.php:
    Code:
    <?php
    	include_once("../scripts/MenuBar.class.php");
    	
    	$menu = new MenuBar();
    	$menu->add("Google", "http://www.google.co.cr/", "_blank");
    	$menu->show();
    ?>
    The Apache return the follow error:

    Fatal error: Call to a member function on a non-object in /home/iconco/public_html/invent/scripts/MenuBar.class.php on line 25

    Can Helpme??
    Last edited by wilz04; March 29th, 2007 at 12:11 PM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Welcome to the forums wilz04. Please do not post over someone else's problem. In fact, you posted on a thread 2 years old. I will have a moderator split this for you.

    Second, please use &#91;code&#93; tags when posting code. It makes things easier to read. You would use them in the following manner.
    &#91;code&#93;
    your code here
    &#91;/code&#93;

    As concerning your problem, the log has given you some help. What is the 25th line of MenuBar.class.php?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2007
    Posts
    13

    Unhappy Re: PHP drives me nuts -> Call to a member function on a non-object

    Yes, I Know.. but the I don't resolve the error.

    I declared all the vars (instances) and call my functions correctly, but the apache continue says: Call to a member function on a non-object!!!!, but it 's a object!!!
    I declare the items array as var of the class MenuBar, and the following code is the body of the her 'show' function, and I assign an array in the constructor of this class.
    In the function 'add', I assign to '$items[0]' the value of '$item', param of the function, then '$items' is not null.
    And 'toString()' is a function of class 'MenuItem', type of '$items', he returns a string.
    I belive that all is good, BUT NOT!!

    MenuBar.class.php (only 'show' function):
    Code:
    function show() {
    	$me = "<table><tr>";
    	$j = count($this->items);
    	for ($i=0; $i<$j; $i++) {
    		$me .= $this->items[$i]->toString();//line of error (25)
    	}
    	$me .= "</tr></table>";
    	echo $me;
    }
    I realy need help!!
    Last edited by wilz04; March 29th, 2007 at 12:39 PM.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Well, where is your function toString()? That is a JavaScript function, but in PHP it does not exist. In fact, variables act differently.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Mar 2007
    Posts
    13

    Re: PHP drives me nuts -> Call to a member function on a non-object

    This is my function 'toString()'.
    She is in the class 'MenuItem'
    the vars '$href', '$target', and '$label was declared as members of this class, and in her constructor I initialized this vars with params values.

    MenuItem.class.php (only 'toString()' function):
    Code:
    function toString() {
    	return "<td><a href='".$this->href."' target='".$this->target."'>".$this->label."</a></td>";
    }
    Help me PeejAvery!!

    Note: Can you write in spanish?
    Last edited by wilz04; March 29th, 2007 at 01:01 PM.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP drives me nuts -> Call to a member function on a non-object

    In your code, $this->items[$i] must be returning a non-object. Have you echoed them to see what they are obtaining?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Mar 2007
    Posts
    13

    Re: PHP drives me nuts -> Call to a member function on a non-object

    I echoed '$this->items[$i]' to see what they are obtaining, and show 'Google'.

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Well, as you can see Google is just the first of three parts of an object/array. Try changing the show function to the following code.

    Como ya puedes ver, Google es solamente la primera de tres partes de un array. Prueba el cambio en la funcion show.

    PHP Code:
    function show() {
        
    $me "<table><tr>";
        
    $me .= $this->items->toString();
        
    $me .= "</tr></table>";
        echo 
    $me;

    EDIT: Por que quieres que escriba yo en el castellano?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Mar 2007
    Posts
    13

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Thanks PeejAvery, I see the error, it's in the menu.php:
    Code:
    <?php
    	include_once("../scripts/MenuBar.class.php");
    	
    	$menu = new MenuBar();
    	$menu->add(new MenuItem("Google", "http://www.google.co.cr/", "_blank"));//line (5) of error (resolved)
    	$menu->show();
    ?>
    previously I send three params at the function 'add()' of class 'MenuItem'
    Before:
    Code:
    $menu->add("Google", "http://www.google.co.cr/", "_blank"));//line 5, bad!
    Now, I send only one param, yes, my 'add()' only recv one param (type of 'MenuItem')!!
    After:
    Code:
    $menu->add(new MenuItem("Google", "http://www.google.co.cr/", "_blank"));//line 5, good!
    Thanks again PeejAvery!!

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Glad you solved it. So why did you want me to write in Spanish?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Mar 2007
    Posts
    13

    Re: PHP drives me nuts -> Call to a member function on a non-object

    Gracias PeejAvery, Si, logre solucionar el problema, pero me ayudaste muchisimo!!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured