PDA

View Full Version : call to non-member function - need help!!



appleifreak
05 Jun 2009, 06:27 PM
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:

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:

function index()
{
$this->load->view('test');
}

view file:

$navInfo = array(
1 => array(
'anchor' => 'profile',
'link' => 'Profile'
),
2 => array(
'anchor' => 'alliance',
'link' => 'Alliance'
)
);

echo $this->ui->navBar('profile','profile',$navInfo); // THIS IS LINE 13

BTW, this is built in the framwork CodeIgniter (http://codeigniter.com/) 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

Alan
06 Jun 2009, 02:51 AM
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.


$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.



$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).


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.



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:


echo $this->navBar('profile','profile',$navInfo); // THIS IS LINE 13

If you have any questions, ask away.