|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
need help with OOP design for my site
Hello:
I am trying to develop my own personal website but I am trying to do it by following OO concepts. Now although technically it shouldnt matter what I am using since the OO design should work with anything this is what I got: - Apache webserver, with myslq database and php on it. The part that I am caught up in right now is in the the design of the database object. I am not sure that I have designed it properly and that I am doing alot of things incorrectly. I have split the database object into 2 classes, the first is the actual database and its properties looks like this: 1. Name: Database() Description: The constructor. It Initializes the $server, $username, $password and $database class members 2. Name: openConnection() Description: Opens the connection to the database server and selects the database (uses the property selectDatabase) 3. Name: selectDatabase() Description: chooses a database to work with 4. Name: queryDatabase() Description: queries the database and initializes the $queryResults class member to the results of the query 5. Name: returnResults() Description: Returns the $queryResults member 6. Name: insertRecord(), deleteRecord(), & modifyRecord Description: These properties pretty much run an SQL statement and return a message saying how many rows were affected (if the query was successful). 7. Name: displayResults() Description: Displays the contents of the $queryResults class member by creating a new ResultsPAge (read below) object and calling its display() property. OK that is pretty much it for the properties, the class members (or class variables) $servername, $username, $password, $dbName and $queryResults. Pretty much all of these (except the $queryResults) are initialized set by the constructor. The other object or class that I have that goes hand in hand with the database object is the ResultsPage object and this is what I am having problems with. In essence I created the ResultsPage object in order to have a flexible way of displaying whatever results get returned from an SQL query. The main purpose of the ResultsPage is that if the number of results that are returned is very high it displays the webpage with the ability to page through the results. When I thought about it, the scope of what this does is really what a WebPage object would except that it is pretty specialized to displaying records only so it didnt quite seem to fit in the scope of a Database object (which is only supposed to connect to a database, and query it) so that is why I made it its own object. The properties in the ResultsPage object look like this: 1. Name: ResultsPage($recordSet,$pageSize) Description: The constructor. Initializes the $recordSet, $pageSize, $numberOfPages, $numberOfRecords, and $currentPage class members 2. Name: setCurrentPage($page) Description: Sets the class member $currentPage 3. Name: setStyles($headerStyle,$recordStyle,$recordAlterna teStyle,$buttonStyle,$frmElementStyle) Description: This property sets all of the styles that will be sed to display the page. 4. Name: display() Description: this is the function that creates ALL the HTML to lop through the recordset to create the page. If it happens that record paging functionality needs to be added this function also displays a record navigating form at the bottom of the page so that a user can jump from page to page. The class members or variables for the ResultsPage class are: $headerStyle, $recordStyle, $recordAlternateStyle, $buttonStyle, $frmElementStyle, $recordSet, $currentPage So by now you are all probably shaking your head and saying "he should have never picked up an OOP book." As you can tell I need help. From what I can see my main problem is the display() function of the ResultsPage object, if you were to see the implementation code for it you would see that it is about 50 lines long and it has so much embeded HTML and echo statements I dont know if I should be separating all of this or what. The main reason why I have done it like this is because there is a specific way in which I want the results pages to look, if there are more than so many records than there is a specific way that I want it to be able to page. and dont get me all wrong, I actually managed to get it to work. But when I am creating the database object and the pageresults object on the page that the records will be displayed from it seems like I have to end up doing more work than I should. I am going to post the implementation code for the resultspage object below if you want me to post the code for the Database object I can do so as well. ResultsPage: PHP Code:
Last edited by particleman : September 17th, 2003 at 12:23 PM. |
|
#2
|
|||
|
|||
|
and I also decided to post the code for the database object:
PHP Code:
and a page where I would use the database object would look like: PHP Code:
Last edited by particleman : September 17th, 2003 at 12:25 PM. |
|
#3
|
|||
|
|||
|
Just a couple more things to mention here are that I know that is alot of code and I dont expect anyone to go through it and tell me what lines to change. What I want is an idea of how I could change the design to resemble more of an OO design and make it more efficient, the code is pretty much documentation to show you what I have done. There are a few more properties in the code that I didnt mention only because they werent that relevant. Either way any help at all would be appreciated.
There is another object in those pages if you have noticed it is a Form object which is used as the navigation form for the records. Thanx in advance for any help you can give me. |
|
#4
|
||||
|
||||
|
You might consider consolidating some of your query functions into one, as they do the same thing (execute and test the success of a query passed as a parameter to the function). You're probably never going to extend these functions, so why not consolidate?
In my own db class, I've got a function called query() that lets me do an ad hoc insert, update, delete, etc. basically, I expect to get nothing back from this function, and if I get anything back but true, I know I've gotten an error. I've also got a fetch() function that returns an array of row arrays. This allows me to do nifty things like PHP Code:
At a glance, I didn't see any really major issues with your code, though. You'd probably do better to call one a database object and the other a page object, and as a matter of lingo, I don't think you can really talk about splitting an object into two classes, as a class is simply the definition of a single object. |
|
#5
|
|||
|
|||
|
dhouston: Thanx for the reply I' really new at using OO concepts in my programming.
Just a couple more questions though. I noticed that you mentioned that you had a database class. Well how do you deal with the display of the password. For instance in the bit of my code where it says PHP Code:
on the page where I would use the database object, is there a way of not SHOWING the password? I mean can I make a connection to the database wihout having the actual text password in the script, I guess something like using a DSN on an IIS server. I havent been able to find something like that whilst using a LAMP system. |
|
#6
|
|||
|
|||
|
Personally I put information such as passwords in a config file that is then included so you can reference the password as $password, $config ['password'] or something similar.
If all the project files are available to someone this obviously won't help but does mean you can release all the source to anyone (minus the config file) and they won't have access to the database. It also makes changing the details much easier since they are all kept in one place. Hope this helps, -KM- |
|
#7
|