jamina1 Posted June 4, 2007 Share Posted June 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 id remove the global scope and just do $database = new mssqlDB; inside of your countNews method Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 4, 2007 Author Share Posted June 4, 2007 Then it tells me: Fatal error: Cannot instantiate non-existent class: mssqldb in D:\wwwroot\undergraduateStudies.ucf.edu\cpanel\includes\news.php on line 10 It thinks db.php doesn't exist? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 include it at the top of news.php Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 4, 2007 Author Share Posted June 4, 2007 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? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 did you remove /* Create Database Connection */ $database = new mssqlDB; and remove the GLOBAL in the countNews method? Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 4, 2007 Author Share Posted June 4, 2007 Why won't it see the database declaration in the db.php file? I'm going to need the class in other files, so I can't just declare it only in news.php or else I'll run into the redeclaration problem again. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 4, 2007 Author Share Posted June 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 instead of doing it twice, why not make it a property of that class class news{ var $database = ''; function fetchNews($page) { $this->database = new mssqDB; ... and when you need to use it again, just reference $this->databse Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 4, 2007 Author Share Posted June 4, 2007 Should I then use $this->database->query() as the structure to get to the methods in the database class? It still is giving me the non-existent class error, and I did exactly what you said Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 there i no need for the colon at the end of your classes, maybe that is causign the errors Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 4, 2007 Author Share Posted June 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted June 4, 2007 Share Posted June 4, 2007 put this at the top of each page error_reporting(E_ALL); and then browse directly to that script and see what the errors are Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 5, 2007 Author Share Posted June 5, 2007 No errors on either news.php or db.php. Only errors I see are the persistent call to function on non-object messages. Quote Link to comment Share on other sites More sharing options...
jamina1 Posted June 5, 2007 Author Share Posted June 5, 2007 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.