Classes / OOP Programming error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Classes / OOP Programming error

Post by fatal »

I get this error from my that it cant find this once function, but others i call before it are fine. The error is:
Fatal error: Call to a member function on a non-object in c:\apache\apache\htdocs\testnews.php on line 41
The code for testnews:

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<table width="502" cellspacing="0" cellpadding="0" align="center" border="1" bordercolor="#000000">
  <tr> 
    <td width="500" bordercolor="#000000" bgcolor="#6699CC">
      <blockquote>
	  <?php
	  		include_once("news_sys.php");
			#news_sys = new news_sys;
			
			switch( $p )
			&#123;
				case "comment":
					$news_sys->GetComments($ID);
					echo"
					<form name="form1" method="post" action="$PHP_SELF?p=add">
  						<p><font face="Tahoma" size="2" color="#000000">Subject: 
    						<input type="text" name="subject" value="Re: $subject">
    						<br>
							Comments: 
							<textarea name="comments" cols="25"></textarea>
							<input type="hidden" name="ID" value="$ID">
    						</font></p>
  						<p> 
    						<input type="submit" name="Submit" value="Submit">
    						<input type="reset" name="Submit2" value="Reset">
  						</p>
					</form>
					";
					break;
				case "add":
					$news_sys->InsertComment( $subject, $comments, $ID );
					$subject = $comments = $ID = "";
					break;
				default:
					$news_sys->GetNews(15);
					break;
			&#125;	
	  ?>
	  </blockquote>
    </td>
  </tr>
</table>
</body>
</html>
the particular line of error is:

Code: Select all

$news_sys->GetNews(15);
I have no idea whats going wrong, bec the other class calls are fine. Im using Apache 1.32.24, and Php 4.3.0dev

Thanks for anytype of help.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

On line 14:

Code: Select all

#news_sys = new news_sys;
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

when i uncomment it, i get errors for line 14.
Parse error: parse error in c:\apache\apache\htdocs\testnews.php on line 14
my class is called 'news_sys' if thats any help.

also another weird thing about it is when I make my include file not in class sturcture, just functions. It still cant find the function on line 41.

Im powerfully confused on this one.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Code: Select all

#news_sys = new news_sys;
should be changed to

Code: Select all

$news_sys = new news_sys;
You can't just uncomment it, you also have to assign it to a real variable.

Also, if you have a constructor for your class, you need to do this:

Code: Select all

$news_sys = new news_sys();
at least.
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

Thanks Jason, i am now past all the errors i gotten previously. Thanks again.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

What was the problem?
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

my stupidity :?
Post Reply