akurtula Posted July 8, 2009 Report Share Posted July 8, 2009 hi after trying out the tutorials on oop by stefan, i then started to convert one of my shopping cart demos using oop php luckly, i think that i got the hang of it, : this is one of my classes: (if there's anything wrong plz let me know) class DbCon { function DbCon($db_host="localhost", $db_user="root", $db_pass="", $db="university") { mysql_connect($db_host, $db_user, $db_pass) or die( "Unable to connect to SQL server"); mysql_select_db($db) or die( "Unable to select database"); $query = "SELECT * FROM kurtula_shop "; $this->result = mysql_query($query); } function viewResult(){ while ($row = mysql_fetch_array($this->result,MYSQL_ASSOC)) { echo " $row[item] $row[description] $row[price] / $row[quantaty] "; } } }//end conect class the above class, does work fine and defenately can see the advantages of using oop. however, referring back to the video tutorials, Stefan clearly mentioned that in the class we should not use echo , or set html tags (which does make sense) though would i be right in thinking that this is one of those situations that (as far as basic) there is no other way of achieving the above, other than echoing the html in the class. Q2: is there a rule of thump into how long a class should be (how many functions should it contain) Q3: should each class be in a separate file or can they all be in one file? (whats the difference) thanks a lot Quote Link to comment Share on other sites More sharing options...
russellharrower Posted July 11, 2009 Report Share Posted July 11, 2009 Hi, i like your script, i have been having issues connecting with the DB using OOPS however i seem to get a blank page when i use your script. I understand why i am getting a blank page, due to i have not called the class, but when i try to call the class i can't. I was wondering if you would kindly show us, how you would call the viewResult... Thanks Russell H. Quote Link to comment Share on other sites More sharing options...
quyen Posted July 11, 2009 Report Share Posted July 11, 2009 Hi, I think Stefan's right, After you read some more about Model - Control - View, You may understand what does he mean by that. For now I have some comment for you like these: for Q2: Acctually I don't think there are any rule to say that your class has to be this long or that long. Number of fields and methods in a class, it depends on your object you are creating. for Q3: You should read some more about OOP. if you understand OOP then you might have something to do with this object (the object i mentioned here is about the object in your question Q3). How to organize your php files??? (0_0). Let's think about your question. You can have different classes in a file. thats ok. Lets think your php file (You have different classes in this file) look like a library php file wherever you want to create an object, you can include that library file in and create your object. You also create different library php files depend on how you want to organize your projects. Thats all I can say now. Hope it can help you. Sorry my english is pretty bad, then still hope that you can understand what i mean. Quyen, Quote Link to comment Share on other sites More sharing options...
russellharrower Posted July 11, 2009 Report Share Posted July 11, 2009 Hi, I think Stefan's right, After you read some more about Model - Control - View, You may understand what does he mean by that. For now I have some comment for you like these: for Q2: Acctually I don't think there are any rule to say that your class has to be this long or that long. Number of fields and methods in a class, it depends on your object you are creating. for Q3: You should read some more about OOP. if you understand OOP then you might have something to do with this object (the object i mentioned here is about the object in your question Q3). How to organize your php files??? (0_0). Let's think about your question. You can have different classes in a file. thats ok. Lets think your php file (You have different classes in this file) look like a library php file wherever you want to create an object, you can include that library file in and create your object. You also create different library php files depend on how you want to organize your projects. Thats all I can say now. Hope it can help you. Sorry my english is pretty bad, then still hope that you can understand what i mean. Quyen, Sorry?? i am not understanding what you are talking about... Quote Link to comment Share on other sites More sharing options...
quyen Posted July 11, 2009 Report Share Posted July 11, 2009 Hi, You know I don't speak English well, I was replying to Akurtula, and I just tried to give him some of my ideas. Sorry if I didn't explain it that well. Quote Link to comment Share on other sites More sharing options...
akurtula Posted July 12, 2009 Author Report Share Posted July 12, 2009 (edited) Hi, I think Stefan's right, After you read some more about Model - Control - View, You may understand what does he mean by that. Sorry my english is pretty bad, then still hope that you can understand what i mean. thanks for the great reply, first of all, the english is pretty good. i agree that i need to ready a lot more on opp. basically the way i thought of Q3 (adding to what you mentioned) , is that if 2 classes cant work without one another, then they should be left in one file, though if one class works out the total of a shopping card and the other connects the database, then they should be in separate files as they can be reused separately - By the way, is there any good book that i should read on OOP Modules - control - view and so on .. ? again thanks Edited July 12, 2009 by akurtula Quote Link to comment Share on other sites More sharing options...
akurtula Posted July 12, 2009 Author Report Share Posted July 12, 2009 <?php echo $conection->viewResult();?> 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.
Note: Your post will require moderator approval before it will be visible.