Ok so i am about to start codeing a website and i decided to do it using OOP. I understand HOW to program in oop but i don't understand how to design in oop. For example lets say i want to make a simple guest book with a reply center like here on code call, what is the object? Is it the guest book altogether, is it say the reply center one and the posts another, is it portions of the reply center being multiple objects? I am confused on how to aproach this. I know my Java book explains how to design a dog object and i understand that but when you get to things a little more complex than a dog i don't know how to break it apart. can some one explain it to me?
OOP?
Started by zeroradius, Jan 15 2010 09:31 PM
16 replies to this topic
#1
Posted 15 January 2010 - 09:31 PM
|
|
|
#2
Posted 16 January 2010 - 05:07 AM
Think in terms of "What are reusable, non-trivial chunks of code that will need to be displayed, manipulated, or stored?" Those would be your candidates for objects.
#3
Posted 16 January 2010 - 09:13 AM
So it would be like, the reply center is an object because i can use it in the guestbook, forum, and so on? or am i still thinking to big? I'm still a bit hazy on how to break things apart.
#4
Posted 16 January 2010 - 09:27 AM
In my opinion, textbooks aren't very good at explaining the purpose of OOP in detail. The #1 advantage of OOP is data abstraction and reusability of code.
With that said, what the objects are depends on what language you're using. In Javascript the objects are mainly HTML elements, but it sounds like you're talking about PHP, in which case I don't know.
With that said, what the objects are depends on what language you're using. In Javascript the objects are mainly HTML elements, but it sounds like you're talking about PHP, in which case I don't know.
Life's too short to be cool. Be a nerd.
#5
Posted 16 January 2010 - 11:41 AM
Assuming this is all about connecting a database backend to an HTML/form front-end, I'm guessing that your Objects are going to act as wrappers for form data to send it to the database, and wrappers for database data to send it to HTML.
Methods that come to mind: DBread, DBwrite, formwrite, formread, HTMLpost.
Methods that come to mind: DBread, DBwrite, formwrite, formread, HTMLpost.
#6
Posted 18 January 2010 - 03:28 PM
Thanks for the replies. I read a few articles and I understand a bit better now. With that understanding I am begining to relise althou useful in somthing such as Java, c#, and so on; OOP looks like it won't be to useful in web dev (at least not in php), I can develop reusable code much quicker the way i have been doing it.
#7
Posted 18 January 2010 - 03:57 PM
Well, I'm doing my very first OOP project in PHP now, and truly see some advantages, and one is the inheritages. you program a general class, then you inherit it to a specific usage class, reuse what can be reused and overload the methods that can't.
for example, database tables are often similar to eachother, they has a name, and a number of columns, whereof one is an id column, and most database tables, you treat the very same way, adding, reading, updating or deleting. in most cases, you don't need anything more than an array of the wanted info sent to and from this class... then inherit it and change one method if you wan't delete to not actually delete, but to set a delete flag on the row in the database instead. all other functions is intact...
this is my super class I'm working on right now. the code isn't tested fully yet, but you will get the hang of it...
for this project, I've made a singleton class for the database needs, db...
for example, database tables are often similar to eachother, they has a name, and a number of columns, whereof one is an id column, and most database tables, you treat the very same way, adding, reading, updating or deleting. in most cases, you don't need anything more than an array of the wanted info sent to and from this class... then inherit it and change one method if you wan't delete to not actually delete, but to set a delete flag on the row in the database instead. all other functions is intact...
this is my super class I'm working on right now. the code isn't tested fully yet, but you will get the hang of it...
class Datamodel {
private $user;
private $table;
public function __construct(User $u, $t) {
$this->user = $u;
$this->table = $t;
}
protected function log($id, $what) {
db::query("INSERT INTO logs VALUES (0, '".db::escape($this->table).
"', '".db::escape($id)."', '".$this->user->getId()."', '".db::escape($what).
"')");
}
public function add($info) {
$sql = "INSERT INTO ".$this->table." SET ";
$first = true;
foreach ($info as $var => $val) {
if (!$first) {
$sql .= ", ";
}
$first = false;
$sql .= $var."='".db::escape($val)."'";
}
db::query($sql);
$this->log(db::insert_id(), "Added");
return db::insert_id();
}
public function delete($id) {
db::query("DELETE FROM ".$this->table." WHERE id='".$id."'");
$this->log($id, "Deleted");
}
public function update($id, $info) {
$sql = "UPDATE ".$this->table." SET ";
$first = true;
foreach ($info as $var => $val) {
if (!$first) {
$sql .= ", ";
}
$first = false;
$sql .= $var."='".db::escape($val)."'";
}
$sql .= " WHERE id='".db::escape($id)."'";
db::query($sql);
$this->log($id, "Updated");
}
public function read($id) {
$res = db::query("SELECT * FROM ".$this->table." WHERE id='".db::escape($id).
"'");
if ($res->num_rows != 0) {
return $res->fetch_assoc();
} else {
return null;
}
}
public function getList() {
$res = db::query("SELECT id FROM ".$table);
while ($row = $res->fetch_assoc()) {
$data[] = $row['id'];
}
return $data;
}
public function getAll() {
$res = db::query("SELECT * FROM ".$table);
while ($row = $res->fetch_assoc()) {
$data[$row['id']] = $row;
}
return $data;
}
}
for this project, I've made a singleton class for the database needs, db...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#8
Posted 18 January 2010 - 04:09 PM
hmm, good point but is it worth taking the extra time to create a class for something so simple? It only takes seconds to write a query and make it run. Then again i guess i do need to learn it sence all the employers are wanting it :thumbdown:
#9
Posted 18 January 2010 - 04:25 PM
well, next time I'll use an database table, I just code five lines:
then I just use different instances to do my queries on different tables.
sure, you could do instances from the Datamodel direct for these simple ones,
but say you want to modify only one thing, then you just rewrite one part of it, but it uses the very same interface
class MyData extends Datamodel {
public function __construct(User $u) {
parent::__construct($u, "MyData_tablename");
}
}
then I just use different instances to do my queries on different tables.
sure, you could do instances from the Datamodel direct for these simple ones,
but say you want to modify only one thing, then you just rewrite one part of it, but it uses the very same interface
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#10
Posted 18 January 2010 - 04:42 PM
I guess it could be useful. Everything takes more time the first time you try it i rekon. I am going to try a simple project with it before I start my biger project as i want the bigger one for my portfolio it would be nice to be able to say it is done in OOP. One last question, When you do OOP for web do you usualy make one file with all the classes, one folder with a seprate file for each class or just let them float around with all your other source files?
#11
Posted 18 January 2010 - 09:56 PM
I personally do it java style with one class per file, and then I've implemented an __autoload() so I don't have to include all these files automatically
this autoload loads a class in any first level subdir to "modules" dir, for class files having the named as the class and using one of the four extentions in the foreach...
this autoload loads a class in any first level subdir to "modules" dir, for class files having the named as the class and using one of the four extentions in the foreach...
function __autoload($class) {
//echo "Loading class ".$class."<br/>\n";
foreach (glob("modules/*", GLOB_ONLYDIR) as $dir) {
foreach (array(".php", ".class.php", ".inc", ".class.inc") as $ext) {
if (file_exists("./".$dir."/".$class.$ext)) {
include ("./".$dir."/".$class.$ext);
// echo "File ".$dir."/".$class.".".$ext." was autoloaded!<br />\n";
return true;
}
}
}
return false;
}
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
I study Information Systems at Karlstad University when I'm not on CodeCall
#12
Posted 18 January 2010 - 10:40 PM
can any1 tell me a good tutorial for oops?


Sign In
Create Account


Back to top









