Object Oriented PHP using NetBeans

Hi everyone.

I normally code using structural/procedural programming and have only recently started to look into object oriented PHP. I am using netbeans and want to incorporate the MVC model.

I am trying to understand how it all works and the way in which i link them together. I think it is not too difficult to get my head around it all but i was hoping that there would be a tutorial that i could follow so understand how it all works.

For example, which files would go into the model, view and controller? Also how the syntax changed etc…

Can anyone help me with this?

Regards
Billy

Uh, NetBeans is just an IDE that you press keys in. It has nothing to do with MVC or OOP, at all.

ok maybe i was slightly vague. I will netbeans as the software to build websites.

BUT…

i need to understand how MVC works in terms of php and the way in which the files are set up. For example, i usually have all of my sql queries, design etc on one page. This is changed when using oop and mvc…

So are there any tutorials etc i can look at as i cant seem to find any good ones.

There are plenty of good tutorials on the web and topics on SitePoint. Also see the Symfony link in my sig for an open-source MVC framework – that should give you a good idea.

Basic MVC in 5 minutes huh?

view-entry.php?id=1


$controller = new BlogController(
    new BlogModel(),
    new Template('blog.view.entry')
);

echo $controller->viewEntry($_GET['id']);

Model


class BlogModel
{
    public function findOneById($id){
        return array(
            'id'        => 1,
            'title'     => 'Hello World',
            'date'      => date('r'),
            'content'   => 'Just another hello world example.'
        );
    }
}

View


<h1 id="post-{{id}}">{{title}}</h1>
<p>{{content}}</p>
<p>Posted by admin on {{date}}</p>

Controller


class BlogController
{
    protected
        $model,
        $view;
        
    public function __construct($model, $view){
        $this->model = $model;
        $this->view = $view;
    }
    
    public function viewEntry($id){
        return $this->view->assign($this->model->findOneById($id))->render();
    }
}

Template


class Template
{
    protected
        $template,
        $data       = array();
    
    public function __construct($template_file){
        $this->template = file_get_contents(sprintf('&#37;s.tpl', $template_file));
    }
    
    public function assign($data){
        $this->data += $data;
        return $this;
    }
    
    public function render(){
        $compiled = $this->template;
        foreach($this->data as $key => $value){
            $compiled = str_replace(
                sprintf('{{%s}}', $key),
                $value,
                $compiled
            );
        }
        return $compiled;
    }
}

Output

<h1 id="post-1">Hello World</h1>
<p>Just another hello world example.</p>
<p>Posted by admin on Sun, 07 Feb 2010 19:45:59 +0000</p>

Beautifully explained Anthony.

I want to become a PHP ninja like you! :wink:

Now, were you to query a database to pull the contents would you put your SQL inside the model?

I am also just starting to get to grips with MVC.

Yes, the model is where the SQL would go.

Gee, thanks.

Yes, the model is responsible for retrieving/providing data. A rough non-functional example would be…


<?php
class DatabaseBlogModel
{
    public function findOneById($id){
        $result = $this->database->execute(
            sprintf(
                'SELECT id, title, date, content FROM blog WHERE id = %d LIMIT 1',
                $id
            )
        );
        return $result->fetch();
    }
} 
?>

Same here josebb, i currently program procedurally but have been learning MVC and it looks like its the way to go :wink:

I will try AnthonySterling’s(aka SilverBullet ??) kindly suggested example and see how it all works…

MVC is just a design pattern for Object Oriented Programming (OOP). what you are really doing is learning OOP techniques and then applying the MVC design pattern to suit the application. You could apply any other design pattern if it suited the task, but the base code will always be OOP.