Using mvc in Small scriptlets

Hi…,

Since i m involved in developing some small scripts in php could anybody please guide me to implement mvc in small scripts(with some example template) and suggest me whether the such implementation is worth …? the scripts may have 70 lines of code and even larger…

thanks in advance

If you have a small script, then basing it on an MVC application (or even developing it with object oriented concepts) is rather hard to justify unless said small scripts can work together as plugins to a standing application.

The reason being that the strength of using OOP is flexibility - commonly over-flexible applications which introduce complications. For example, in a full-scale application you may want the option to use multiple data sources, so you’d possibly use a dependency-injection approach as below to do such a thing:

<?php
interface iDataStore{
    public function find($Table, array $Clause = array(), $Limit = '', $Order = '');
    public function joinFind(array $Tables, array $JoinClause = array(), array $Clause = array(), $Limit = '', $Order = '');
    public function save($Table, array $Fields);
    public function delete($Table, array $Clause, $Limit = '');
    public function getTableName();
    public function getRecordType();
}
interface iDataMapper{
    public function __construct(iDataStore $DataStore);
    public function find(array $Clause = array(), $Limit = '', $Order = '');
    public function save(array $Fields);
    public function delete(array $Clause);
    public function getTableName();
    public function getRecordType();
}
interface iDataRecord{
    public function getIdentifier();
    public function getFieldArray();
    public function getField($FieldName = null);
}

So that you can have multiple mappers going around with different data sources.

Then of course you want to set up views and actions to go with them, so a directory structure I’d use is something like:


System/
    Classes/
        Application.php
    Interfaces/
        iDataRecord.php
        iDataMapper.php
        iDataStore.php
    Plugins/
        Product/
            Classes/
                ProductRecord.php
                ProductMapper.php
                ProductController.php
            Admin/
                Actions/
                    Default.php
                    Edit.php
                    Add.php
                    Install.php
                Views/
                    Default.php
                    Edit.php
                    Add.php
                    Install.php
            Views/
                Default.php
                View.php
    Functions.php
    Settings.ini

That would be the structure of a flavour of MVC which I’ve adapted to my coding style. With a full-scale application with many plugins (so-called because they can be dropped into another application and when the Install file has been run it would run), a structure and system such as the above is perfectly justifiable.

Ok, so I think it’s about time I get to my point. With a simple script you’d be using one or two of these plugins, therefore using such a framework is completely and utterly pointless. You wouldn’t need to encounter multiple data sources, you wouldn’t really need to have a controller and a few dozen views/actions, because it would defy the classification as ‘a simple script’. Then you have to think about what happens when multiple of these things you’ll have in a website - you have to remember that a typical user would want to put it into a directory and be able to run it, so said user would probably have multiple frameworks in separated directories, again defying the point (in my perspective anyway) of a framework.

So in conclusion I’ll leave you with some advice - don’t get into using techniques designed for large applications for small scripts. That’s like driving a tractor to work every day - it has a lot of pushing power and can handle large loads, but when you need it for simple transportation it makes things a lot less economic and definitely slower.