PHP gurus - please critique my OOP programming
I'm new to OOP. I'm trying to improve my coding skills & habits as much as possible.
I'm giving you a basic example of a dummy class to manage posts that has methods Save, Delete, Fetch All, fetch by id. I want you to point out any mistakes I'm making, and any code improvements I can make to optimise the way I code.
I'm looking to implement some sort of error catch (which I've attempted) but need guidance in this area.
<?php class Post { var $error = NULL; function Post() { } function save($title, $post_date, $content, $categories, $status, $url) { if(get_magic_quotes_gpc()) { $title = stripslashes($title); $contact = stripslashes($contact); } //check required fields aren't empty $this->isEmpty($title); $this->isEmpty($post_date); $this->isEmpty($content); $name = mysql_real_escape_string($title); $email = mysql_real_escape_string($content); $query = "INSERT INTO ".TBL_POSTS."(title, post_date, content, date_created, status, url) VALUES('$title', '$post_date', '$content', '$date_created', '$status', '$url')"; $result = mysql_query($query); $data = mysql_fetch_object($result); } function delete($id) { $query = "DELETE FROM ".TBL_POSTS." WHERE id = '$id"; $result = mysql_query($query); if (!$result) { $this->error = 1; } } function isEmpty($field) { if ($field == NULL or $field == '') { $this->error = 1; } } function fetchAll() { $query = "SELECT * FROM ".TBL_POSTS; $result = mysql_query($query); $data = mysql_fetch_array($result); if (mysql_num_rows($result) == 0) { $this->error = 1; } else { return $data; } } function fetchAllPosted() { $query = "SELECT * FROM ".TBL_POSTS." WHERE post_date <= '$today'"; $result = mysql_query($query); $data = mysql_fetch_array($result); if (mysql_num_rows($data) == 0) { $this->error = 1; } else { return $data; } } function get($id) { $query = "SELECT * FROM ".TBL_POSTS." WHERE id = '$id"; $result = mysql_query($query); $data = mysql_fetch_array($result); if (mysql_num_rows($data) == 0) { $this->error = 1; } else { return $data; } } function getByUrl($url) { $query = "SELECT * FROM ".TBL_POSTS." WHERE url = '$url"; $result = mysql_query($query); $data = mysql_fetch_array($result); if (mysql_num_rows($data) == 0) { $this->error = 1; return false; } else { return $data; } } }
This signature intentionally left blank.
Easy email marketing automation without moving your lists.
Easy email marketing automation without moving your lists.
Easy email marketing automation without moving your lists.