开发者

PHP MVC Framework Structure

I am sorry about the amount of code here. I have tried to show enough for understanding while avoiding confusion (I hope). I have included a second copy of the code at Pastebin. (The code does execute without error/notice/warning.)

I am currently creating a Content Management System while trying to implement the idea of Model View Controller. I have only recently come across the concept of MVC (within the last week) and trying to implement this into my current project.

One of the features of the CMS is dynamic/customisable menu areas and each feature will be represented by a controller. Therefore there will be multiple versions of the Controller Class, each with specific extended functionality.

I have looked at a number of tutorials and read some open source solutions to the MVC Framework. I am now trying to create a lightweight solution for my specific requirements. I am not interested in backwards compatibility, I am using PHP 5.3. An advantage of the Base class is not having to use global and can directly access any loaded class using $this->Obj['ClassName']->property/function();.

Hoping to get some feedback using the basic structure outlined (with performance in mind). Specifically;

a) Have I understood/implemented the concept of MVC correctly?

b) Have I understood/implemented Object Orientated techniques with PHP 5 correctly?

c) Should the class propertise of Base be static?

d) Improvements?

Thank you very much in advance!

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

/* A "Super Class" that creates instances */
class Base {

    public static $Obj = array(); // Not sure this is the correct use of the "static" keyword?    
    public static $var;

    static public function load_class($directory, $class)
    {
        echo count(self::$Obj)."\n"; // This does show the array is getting updated and not creating a new array :)
        if (!in_array($class, self::$Obj))  //dont want to load it twice
        {
            /* Locate and include the class file based upon name ($class) */
            return self::$Obj[$class] = new $class();
        }
        return TRUE;
    }
}

/* Loads general configuration objects into the "Super Class" */
class Libraries extends Base { 
    public function __construct(){
        $this->load_class('library', 'Database');
        $this->load_class('library', 'Session');
        self::$var = 'Hello World!';    //testing visibility
        /* Other general funciton classes */
    }
}

class Database extends Base {
    /* Connects to the the database and executes all queries */
    public function query(){}
}

class Session extends Base {
    /* Implements Sessions in database (read/write) */
}

/* General functionality of controllers */
abstract class Controller extends Base {
    protected function load_model($class, $method) {
        /* Locate and include the model file */
        $this->load_class('model', $class);
        call_user_func(array(self::$Obj[$class], $method));
    }
    protected function load_view($name) {
        /* Locate and include the view file */
        #include('views/'.$name.'.php');
    }
}
abstract class View extends Base { /* ... */ }
abstract class Model extends Base { /* ... */  }

class News extends Controller {
    public function index() {
        /* Displays the 5 most recent News articles and displays with Content Area */
        $this->load_model('NewsModel', 'index');
        $this->load_view('news', 'index');
        echo self::$var;
    }

    public function menu() {
        /* Displays the News Title of the 5 most recent News ar开发者_开发问答ticles and displays within the Menu Area */
        $this->load_model('news/index');
        $this->load_view('news/index');
    }
}
class ChatBox extends Controller { /* ... */  }
/* Lots of different features extending the controller/view/model class depending upon request and layout */

class NewsModel extends Model {
    public function index() {
        echo self::$var;
        self::$Obj['Database']->query(/*SELECT 5 most recent news articles*/);
    }

    public function menu() { /* ... */  }
}


$Libraries = new Libraries;

$controller = 'News'; // Would be determined from Query String
$method = 'index'; // Would be determined from Query String

$Content = $Libraries->load_class('controller', $controller); //create the controller for the specific page

if (in_array($method, get_class_methods($Content)))
{
    call_user_func(array($Content, $method));
}
else
{
    die('Bad Request'. $method);
}

$Content::$var = 'Goodbye World';
echo $Libraries::$var . ' - ' . $Content::$var;
?>

/* Output */
0
1
2
3
Hello World!Hello World!Goodbye World - Goodbye World


As an amateur user of PHP, i am learning PHP by reading the source code of MVC PHP Framework and articles on the web.

It appears to me that there are no (or very few) open source framework that implements the "Super class" idea, instead, they use the Autoload feature of PHP:

http://www.php.net/autoload
http://www.php.net/spl_autoload_register

Also, from what I learnt in the articles and books, it is said that the use of "Super class" is a bad practice.

I think you may try to improve the code by implementing autoloading.

You may also take a look on Kohana at www.kohanaphp.com.
The Kohana 2 series is a PHP-5 only CodeIgniter rewrite plus improvement,
while the Kohana 3 series is a brand new framework with a different architecture.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜