|
-
March 29th, 2007, 11:29 AM
#1
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.
-
March 29th, 2007, 11:36 AM
#2
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 [code] tags when posting code. It makes things easier to read. You would use them in the following manner.
[code]
your code here
[/code]
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.
-
March 29th, 2007, 12:20 PM
#3
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.
-
March 29th, 2007, 12:38 PM
#4
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.
-
March 29th, 2007, 12:43 PM
#5
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.
-
March 29th, 2007, 01:05 PM
#6
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.
-
March 29th, 2007, 01:10 PM
#7
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'.
-
March 29th, 2007, 01:33 PM
#8
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.
-
March 29th, 2007, 01:35 PM
#9
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!!
-
March 29th, 2007, 01:38 PM
#10
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.
-
March 29th, 2007, 01:49 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|