Jump to content

[SOLVED] Call to member function on non-object


jamina1

Recommended Posts

Hi, I'm somewhat new to classes. I've added on to a system that used them and thought I had them down, but when I try to create my own all I get is errors. Here's excerpts of what I have: (Config.php is just defined constants, and shouldn't be causing the issues here)

 

db.php

<?
require("config.php");
class mssqlDB
{
	var $connection;         //The mssql database connection

	/* Class constructor */
	function mssqlDB(){
		/* Make connection to database */
		$this->connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
		mssql_select_db(DB_NAME, $this->connection);
	}

	/* Query Function */
	function query($query){
		return mssql_query($query, $this->connection);
	}

};
/* Create Database Connection */
$database = new mssqlDB;
?>

 

news.php

<?
class News
{				
	/* fetchNews - returns a list of most recent news posts and paginates $perpage, else just returns top 5 posts */
	function fetchNews($perpage) {
	}

	/* countNews - returns how many news posts are currently in the database */
	function countNews() {
		global $database; // Database object
		$q = "SELECT * FROM ".NEWS_TABLE;
		$query = $database->query($q); // <-----Causes problems
		$rows = mssql_num_rows($query);
		return $rows;
	}


};
/* Initialize Class Object */
$news = new News;
?>

 

I also have a file called functions.php, which will be included on every page of the site and holds basic php functions I use down the page. It require()'s both db.php and news.php at the top of the file. Yet, when I load up any page on my site that calls a function using the news class, I get this:

Fatal error: Call to a member function on a non-object in D:\wwwroot\undergraduateStudies.ucf.edu\cpanel\includes\news.php on line 12

Line 12 is the reference to the database class. Why aren't the classes loading properly?

 

Link to comment
Share on other sites

Then it says I can't redeclare the class.. this is being a pain. It recognizes the fact that the class has been declared once so it won't let me redeclare it, but it won't see it in the first place.

 

What gives?

 

Here's how its included:

 

On index.php

require("functions.php")

 

and functions.php looks like this:

<?
session_start();
require("config.php");
require("db.php");
require("news.php");

 

So, why won't it see the database class?

Link to comment
Share on other sites

i think you're having trouble understanding objects and classes

 

a class is a group of procedures designed to handle a task. in layman's terms, an object is an instance of that class.

 

so the point of having a db class is so that it can handle all of the db related calls. you start a new instance of that db class where needed -  you need to use it in your news class - start an instance of it. you need it in another class, start an instance of it. they will be un related, meaning that the work done in instance of the db class your news class will not collide with the work done where it is instantiated anywhere else

 

if you want one global instance of the db class, you'd have to store that in a session and that is a whole other issue

Link to comment
Share on other sites

Ok, I changed News.php to account for your suggestions, but its still giving me an error  >:(

 

db.php:

<?
require("config.php");
class mssqlDB
{
	var $connection;         //The mssql database connection

	/* Class constructor */
	function mssqlDB(){
		/* Make connection to database */
		$this->connection = mssql_connect(DB_SERVER, DB_USER, DB_PASS);
		mssql_select_db(DB_NAME, $this->connection);
	}

	/* Query Function */
	function query($query){
		return mssql_query($query, $this->connection);
	}

	/* Test Function */
	function test() {
		return "This is a test";
	}

};
?>

 

news.php:

<?
include("db.php");
class News
{				
	/* fetchNews - returns a list of most recent news posts and paginates $perpage, else just returns top 5 posts */
	function fetchNews($page) {
		$database = new mssqlDB;
		$RowsPerPage = 10; // Rows Per Page
		$startRows = ($page-1)*$RowsPerPage;
		$q = "SELECT * FROM ".NEWS_POSTS." ORDER BY timestamp DESC";
		$result = $database->query($q);
		$numrows = mssql_num_rows($result);
		if($numrows == 0) {
			return "There are no news posts to display";
		} else {
			@mssql_data_seek($result, $startRows);
			return $result;
		}			
	}

	/* countNews - returns how many news posts are currently in the database */
	function countNews() {
		$database = new mssqlDB;
		$q = "SELECT * FROM ".NEWS_TABLE;
		$query = $database->query($q);
		$rows = mssql_num_rows($query);
		return $rows;
	}


};
/* Initialize Class Object */
$news = new News;
?>

 

And it says: Cannot instantiate non-existent class: mssqldb in news.php in both functions.

Link to comment
Share on other sites

No joy :(

I really don't understand what I'm doing wrong. I'm including both the db.php and news.php files, and it just refuses to see that one class. It recognizes $news->function() calls flawlessly. I just don't get it.

 

Thank you for the great suggestions, so far - it must be my server or something.

Link to comment
Share on other sites

You'll never guess what it was that was wrong -

apparently, the filename I was using was the same as one in the PHPincludes path that I guess it defaults to. So it was looking at the wrong file the whole time, instead of looking at the one I wanted it to.

Thank you for all your help and patience!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.