In my effort to understand MVC in a simple way (I'm trying to make a demo application of my own), I've followed Symfony2's Symfony2 versus Flat PHP material, I've changed some stuff along the way trying to "improve" the code adding some OOP practices, I made a DB class and changed their model from this:
<?php
// model.php
function open_database_connection()
{
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
return $link;
}
function close_database_connection($link)
{
mysql_close($link);
}
function get_all_posts()
{
$link = open_database_connection();
$result = mysql_query('SELECT id, title FROM post', $link);
$posts = array();
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
close_database_connection($link);
return $posts;
}
to this (ignore the spanish please):
<?php
/**
* @author Me
*
*/
/**
* post.php: clase post, elemento de texto básico del blog
*/
class Post
{
/**
* titulo del post
* @var string
*/
private $title;
/**
* Constructor de la clase Post
* @param string $title
*/
function __construct($title)
{
$this->title = $title;
}
/**
* Get para titulo del post.
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set para titulo del post.
* @param string $title
* @return self
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getAllPosts()
{
//Ummm what?
}
}
My problem is, where does this getAllPosts()
method fit in my model post.php?, what am I doing wrong? the only thing that came to mind was creating the method as static but that would make no sense and I kNOW it should not be done like that...
Thanks in advance guys, seems like understanding this whole MVC, MVC-like structures in web development is giving me quite some trouble hehe...
NOTE: This has nothing to do with Symfony at all, I'm just trying to follow their simple MVC-like implementation (incluiding the way they create the Views (which apparently is not the "pure" way MVC should be implemented)).