Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Feb 7, 2010, 10:41   #1
billy_111
SitePoint Wizard
 
billy_111's Avatar
 
Join Date: Jul 2009
Posts: 1,527
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
billy_111 is offline   Reply With Quote
Old Feb 7, 2010, 11:10   #2
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
Uh, NetBeans is just an IDE that you press keys in. It has nothing to do with MVC or OOP, at all.
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 7, 2010, 11:15   #3
billy_111
SitePoint Wizard
 
billy_111's Avatar
 
Join Date: Jul 2009
Posts: 1,527
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.
__________________
PHP/ASP.NET Developer - Happy Ramadan

Kind regards
Billy
billy_111 is offline   Reply With Quote
Old Feb 7, 2010, 11:18   #4
AlienDev
SitePoint Evangelist
 
AlienDev's Avatar
 
Join Date: Feb 2007
Location: UK
Posts: 594
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.
__________________
Me on StackOverflow | Blog & personal website.

I mostly use: PHP, Java, JavaScript, Android.
AlienDev is offline   Reply With Quote
Old Feb 7, 2010, 11:52   #5
AnthonySterling
Twitter: @AnthonySterling
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,053
Basic MVC in 5 minutes huh?

view-entry.php?id=1
PHP Code:

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

echo
$controller->viewEntry($_GET['id']);
Model
PHP Code:

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

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

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
PHP Code:

class Template
{
    
protected
        $template
,
        
$data       = array();
    
    
public function __construct($template_file){
        
$this->template = file_get_contents(sprintf('%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
HTML4Strict Code:
<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>
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja.
Also future, drunk, attendee of the PHPNW10 Conference - come say hello!
AnthonySterling is offline   Reply With Quote
Old Feb 8, 2010, 04:37   #6
josebb
SitePoint Member
 
Join Date: Jun 2009
Posts: 7
Beautifully explained Anthony.

I want to become a PHP ninja like you!

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.
josebb is offline   Reply With Quote
Old Feb 8, 2010, 05:04   #7
smadeira
SitePoint Zealot
 
smadeira's Avatar
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 191
Yes, the model is where the SQL would go.
__________________
Scott
smadeira is offline   Reply With Quote
Old Feb 8, 2010, 05:16   #8
AnthonySterling
Twitter: @AnthonySterling
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,053
Quote:
Originally Posted by josebb View Post
Beautifully explained Anthony.

I want to become a PHP ninja like you!

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.
Gee, thanks.

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

<?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();
    }
}
?>
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja.
Also future, drunk, attendee of the PHPNW10 Conference - come say hello!
AnthonySterling is offline   Reply With Quote
Old Feb 8, 2010, 10:45   #9
billy_111
SitePoint Wizard
 
billy_111's Avatar
 
Join Date: Jul 2009
Posts: 1,527
Same here josebb, i currently program procedurally but have been learning MVC and it looks like its the way to go

I will try AnthonySterling's(aka SilverBullet ??) kindly suggested example and see how it all works..
__________________
PHP/ASP.NET Developer - Happy Ramadan

Kind regards
Billy
billy_111 is offline   Reply With Quote
Old Feb 9, 2010, 16:00   #10
W2ttsy
SitePoint Enthusiast
 
Join Date: Oct 2004
Location: Melbourne, VIC, AU
Posts: 83
Quote:
Originally Posted by billy_111 View Post
Same here josebb, i currently program procedurally but have been learning MVC and it looks like its the way to go

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.
W2ttsy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 01:09.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved