Keeping code and design apart?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
StumpDK
Forum Commoner
Posts: 35
Joined: Thu Feb 12, 2004 2:28 am
Location: Copenhagen, Denmark

Keeping code and design apart?

Post by StumpDK »

Hi!
I've heard alot of people talking about keeping PHP and HTML apart, in different files and so on. I see the logic in this statement, but how do you keep it apart? I've heard something about templates, but I can't figure it out. And are there any other methods of doing so?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

The whole thing is to keep the Model, the View and the Controller separated...
You can get more info on MVC (applyed on J2EE) at
http://java.sun.com/blueprints/patterns ... ailed.html

I developped a FrameWork that tries to bring the MVC concept to PHP.
Isn't released yet, cause I don't get to the time to setup CVS on sf.net and code a small demo app, but I can give you the sources already if you are interested and guide you on it...
I already posted on that at:
viewtopic.php?t=11789&start=0&postdays= ... highlight=
StumpDK
Forum Commoner
Posts: 35
Joined: Thu Feb 12, 2004 2:28 am
Location: Copenhagen, Denmark

Post by StumpDK »

So you are saying that the most effecient (and maybe the only) way to keep the parts seperated, is to use pre-written classes?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Well I might be saying this is the most efficient way.
I shall here not argue OO vs. Procedural
I've made my choice. While it took me years to understand the advantages OO has over procedural...
But I am convienced now!
And patterns are high on that list, MVC bieng one of the most important ones imho
StumpDK
Forum Commoner
Posts: 35
Joined: Thu Feb 12, 2004 2:28 am
Location: Copenhagen, Denmark

Post by StumpDK »

I also believe that OO (if it is as in OOP :D ) is the most efficient way of programming.
You are writing about your program, which isn't done yet, but which other opportunities do I have, if your program isn't done? Are there other classes who takes care of the user-view side of a server program?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

I didn't say it isn't done (well it was not as I wrote those other posts)
But I believe the FrameWork is already useful. I use it on different projects already.
It isn't perfect though and I need pretty much feedback from other users and probably I'll change a few things to it.
In the meantime I am in the process of releasing it on sourceforge (right now).
But as I mentionned you can get the source already if you want. Just CVS will be up as soon as sf.net has accepted the project...
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

BTW
are you dev/deploying on un*x or win?
StumpDK
Forum Commoner
Posts: 35
Joined: Thu Feb 12, 2004 2:28 am
Location: Copenhagen, Denmark

Post by StumpDK »

I would love to try your stuff. I'm using Windows as server OS. Can you mail the files to me?
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Argh... Windows!
I already got your first bug report :
WTF aren't you using DIRECTORY_SEPARATOR
Just that you know ;)

I'm setting up a quick tarball you'll have to work you way through it...
It is available at http://lily.itresolved.org now
There is no demo app right now... And I won't have time to code it before tomorrow or so. There is a small demo vhost config for apache, you might want to look at, uses rewrite rules for i18n.

You'll have to hack yourself through the code... You can fall back on me anytime. Now that you got it, I'm waiting for remarks, bugs, help... all you think is constructiv ;) !
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Be careful with MVC in web apps. The idea of a single controller managing the workflow as well as a model and view (actually there could be several) is both confused and confusing.
Last edited by McGruff on Thu Feb 12, 2004 12:36 pm, edited 1 time in total.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

I don't see what is confused about the MVC pattern... even applyed to web application; while I admit that this can get confusing.
But you are more than welcome to download the code and, as StumpDK is, invited to constructive remarks...
Thanks
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

greenhorn666 wrote:I don't see what is confused about the MVC pattern... even applyed to web application; while I admit that this can get confusing.
Thanks
That's just my take - others might disagree. And this isn't a comment on your code which I haven't seen.

I've got no problem with model & view but alarm bells start ringing when I see how the controller is normally conceived.

In a web app, there needs to be some kind of workflow manager. This might load various objects to carry out pre client-output tasks, print the client output, and finally various post client-output tasks.

Pre and post client-ouput tasks could be silent or they could be "print"-type operations eg email notification, writing log files etc - anything which is read by someone. There could be more than one model and view to manage.

It would seem to drive a coach and horses through the principle of RDD to have a single controller responsible for all that: ie workflow and various unrelated models & views. If the app isn't very complex, design flaws can be concealed - but might not be when you try building on it.

I think it's important to distinguish between workflow management and the various print operations. Print managers (Email, ClientPage etc) could be assigned responsibility for loading a model & view and printing the item. There would be a separate printer object for each print task, each managing a single model and view. In that way MVC is embedded in apps, not entire apps in MVC.

I find it helpful to use a PrintManager interface which encapsulates the model-view separation.

Code: Select all

<?php
class Printmanager
{
    function loadModel() 
    {
        die('Method not implemented<br />.' . __LINE__ . ' | ' . __FILE__);
    }

    function loadView()
    {
        die('Method not implemented<br />.' . __LINE__ . ' | ' . __FILE__);
    }

    function printPage()
    {
        die('Method not implemented<br />.' . __LINE__ . ' | ' . __FILE__);
    }
}
?>
Last edited by McGruff on Tue Aug 09, 2005 6:34 pm, edited 1 time in total.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

My code is now online on sf.net with a demo app.
http://lily.itresolved.org

Just go and see for yourself, I am more than open to remarks, since I want to have it as useful for anyone as possible...
Thanks
eletrium
Forum Commoner
Posts: 34
Joined: Tue Feb 10, 2004 3:38 pm

Post by eletrium »

MVC might be overkill on a lot of PHP pages... but on a full out web application, I can see how it might be useful. Depends on your needs as always...
Post Reply