Reputation: 33
I want to build a simple blog application with PHP using OOP concept. So basically I have a Blog class and a Database Class. In Blog class I have a method called "createANewBlog()" which takes a blog text, and a database connection. for example:
class Blog{
public $id;
public $text;
public function createANewBlog($inText, $db)
{
$this->text = $inText;
$this->id = $db->create($inText);
return true;
}
}
so basically this function call a create() function in database class to get the last inserted Id.
class DB{
function create($intext){
$sql = "INSERT INTO....... ";
then i will send the last inserted id;
}
}
In the index controller I will simply create a blog object and call that method to create a new object.
Now my question is "Is it the right way to code in OOP" or I sould call the Query inside the blog class createANewBlog() function like:
createANewBlog($inText, $db){
$this->text = $inText;
$this->id = $db->execute("INSERT INTO .....");
return true;
}
please help me as I have no idea where is the right place to put the query(Inside the blog class or inside the databse class).
Upvotes: 3
Views: 197
Reputation: 1867
The idea of OOP is to create great application structure. In great application you need to use modular technique. i'll not giving you some big chunk of information which you need to follow . I'll rather giving you a tactics that i followed while i was learning OOP for the first time .
Let me give you an example with code!:
You're working with a database connection , so create a class that will store all the database connection and other connection stuff.
In here i created class called connection_admin
Class connection_admin {
protected $_link;
//If You Delete This Everybody of the Team Will send you a serial killer to kill you ......... :v :v
function __construct($host,$user,$pass,$db){
$this->_link = mysqli_connect($host,$user,$pass);
mysqli_select_db($db,$this->_link);
}
public function query($sql){
$result = mysqli_query($sql);
$this->confirm_query($sql);
return $result;
}
public function confirm_query($sql){
if (!$sql) {
# code...
die('Query Failed'. mysql_error());
}
}
public function count_matched_id($queried_output){
return mysqli_result($queried_output, 0);
}
}
Then i created a global object like "init.php" , which i'll include everywhere in my project.
<?php
include 'function/admin_connection.php';
$Connection = new connection_admin('localhost','root','','my_cart');
And now it's time to require them all in all .php files..
Then in the view files you just need to call $Connection->query() to query database
THADAAAAA!!
Note: it's not a very good use of php's OOP feature , but it'll teach you how to use OOP . after that try to follow MVC structure.
Upvotes: 1
Reputation: 8022
Your structure is some what right but the best practice is to isolate each thing according to their work.
So there would be following things.
DAO (data access object) is an object that provides an abstract interface to database.
So always use DAO to make communication between your model and database. It may contain insert, read, delete, find, filter methods/functions.
Uses of the above things are as follow,
This way you will keep it simple,robust and maintainable.
Upvotes: 4