views:

109

answers:

5

It's been around 5 months since I picked up a PHP book and started coding in PHP. At first, I created all my sites without any organizational plan or MVC. I soon found out that was a pain.. Then I started to read on stackoverflow on how to separate php and html and that's what I have been doing ever since.

Ex: 
profile.php <--this file is HTML,css. I just echo the functions here. 
profile_functions.php <--this file is mostly PHP. has the functions.

This is how I have been separating all my coding so far and now I feel I should move on and start MVC. But the problem is, I never used classes before and suck with them. And since MVC (such as cakephp and codeigniter) is all classes, that can't be good.

My question: Is there any good books/sites/articles that teaches you how to code in MVC? I am looking for beginner beginner books :) I just started reading the codeigniter manuel and I think I am going to use that.

EDIT: Is it possible to have a MVC organization structure to your coding without using cake, codeigniter, etc? Basically just separate say profile.php into 3 different files(the view, controller, model)

A: 

MVC is just a design pattern. It's not really something you can "code in".

If you like to code in PHP, here is an article regarding MVC in PHP. It has an overview explaining the design pattern, and then an example follows.

danben
+1  A: 

How I learned was by going through this tutorial:
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/

The primary focus is to learn the Symfony Framework, but by default, you will be exposed to and learn good MVC principles.

Christopher Altman
Is Symfony another php framework like codeigniter?
ggfan
Yes -- but better -- bring it on ;)
Christopher Altman
+1  A: 

It's not PHP, but see if you can get a copy of Tate's Bitter Java. It will discuss the organizational side of things (how and why the organizational code improves stuff).

I'm a bit hesitant to recommend one of the great Java books for PHP programming, but this book is one of the few that starts with code written without an organizational plan and improves it into a MVC like structure without the use of 3rd party libraries. This way it teaches you what the organization is about from a practical point of view. Hopefully once you understand the pattern, it won't be too difficult to translate the ideas into PHP.

Another alternative is to grab one of the dozens of PHP frameworks and recode to the framework. Doing so will get your results much faster, but the drawback is that you'll likely understand those results in greater detail, and there is a small chance your code will not behave the same after you rewrite it from scratch. We all like to think the new stuff will do everything the old stuff did, but often we forget something (or it behaves differently).

Edwin Buck
+1  A: 

MVC is a "generic" design pattern that is not particular to any language. More of a coding philosophy. On the most basic level it's just separating data from business logic from presentation. Below is a simple example of a "templating" system using MVC. You would be able to swap out any of the parts without breaking anything, and the data is not tied to the formatting/display. This is sample code, not efficient.

Model, get the data:

function getName($id) {
    $name = array('_first_'=>'Joe', '_last_'=>'Smith', '_mi_'=>'C');
    return $name
}

Controller, processes it:

$name = getName(1);
$name['_fullname_'] = $name['_first_'].' '.$name['_mi_'].'. '.$name['_last_'];
outputView($name);

View, output the content:

// Example html file: <b>Hello _fullname_, how are you</b>
function outputView($view, $data) {
    switch ($view) {
    case 'xml':
         $out = file_get_contents('view.xml');
    case 'html':
         $out = file_get_contents('view.html');
    case 'json':
         $out = file_get_contents('view.json');
    }
    $search_for = array_keys($data);
    $replace_with = $data;
    echo str_replace($search_for, $replace_with, $out);
}
Brent Baisley
+1  A: 

to answer your question

Is it possible to have a MVC organization structure to your coding without using cake, codeigniter, etc? Basically just separate say profile.php into 3 different files(the view, controller, model)

absolutely...

first file profile.php ( the view, what gets hit by the browser )

<?php
include( 'controllers/UsersController.php' );
$controller = new UsersController();
$controller->profile();
$pageData = $controller->data;
?>

the controller

<?php
include 'models/UsersModel.php';
class UsersController{

public $data;
public $model;

public function __construct(){
    $this->model = new UserModel();
}

public function profile(){
    $this->data = $this->model->findUser();
}

}

the model

<?php

class UsersModel{

public function __constuct(){
    // connect to your db or whatever you need to do
}

public function findUser(){
    return mysql_query( "SELECT * FROM users WHERE users.id =  2  LIMIT 1" );
}
}
David Morrow