PDA

View Full Version : php structure (MVC?)



Dennis_Gull
09-03-2008, 12:56 PM
Hello everyone :)

Im about to start a new project at home and this time I want to create everything in an organized way and hopefully I learn how to work with PHP OO with css based xhtml code.

Anyways, I know that ASP now is MVC based (Model-view-controller) so that you easily use and reuse componenets and I also heard that PHP doesn't have that good support for this. But I don't know anything about ASP and would rather use PHP so is there some golden rule how you should work in php? :)
I know that static functions is a good option when you work with componenets, for example:

myClass::Button("this is my button label");
Is this the best way to create and recreate componenets in PHP 5? :)

And should I still use the old:

include('right_side.php');

or should I use static functions here too?

Last question:
I want to write everything inside php tags (<?php and ?>)
so how can I keep the xhtml code with a good structure, for example:

echo "<div>...</div>";
echo "<div>...</div>";
would output <div>...</div><div>...</div> in one single line, will I have to add \n in the end of every linebreak I want to make?

I probably have some more questions when I get going but for now this will do.

alexjewell
09-03-2008, 01:51 PM
In reference to your second question, yes - you would have to use \n in order to create a line break. My advice is to not use the php tags for XHTML output. For example:



<?php

// php scripting here

?>

<div>...</div>
<div>...</div>

<?php

//more php here

?>


It's cleaner and faster to parse

Twey
09-03-2008, 02:41 PM
MVC and components are mostly contradictory philosophies when it comes to web design, with MVC being the most popular at the moment. It is not true that PHP doesn't have good support for MVC. PHP's OO is somewhat lacking in a few areas, but MVC does not require OO at all (my favourite implementation in PHP does not use OO), and it appears to be quite possible to hack around these limitations sufficiently in most cases.

include() (or, usually preferably, require_once()) are of course still useful, but you should not be using files with names like 'right_side.php' because this indicates that the code is strongly tied to the output, something that you want to avoid at all costs (and said avoidance is one of the primary motivations for the MVC pattern in the first place).

Likewise, you should not be handling strings of output in your PHP code. Your output must be separated from the rest of your code as much as possible — this is the V part of 'MVC'. You will want to have your 'templates' in completely separate files, probably in a separate directory, and certainly not mixed in with your controllers. A templating engine (http://www.smarty.net/) can be used to greatly ease this separation. If you're working with databases, an ORM (http://propel.phpdb.org/) will also help cut down on the controller code by quite a bit.

At this point in time, it's almost certain that you do not wish to be working with XHTML, as IE has no support for it. http://www.webdevout.net/articles/beware-of-xhtml has some further information on this, if you're interested. You should almost certainly be sticking to HTML 4.01 Strict.

There are a variety of MVC PHP frameworks available pre-written for you (http://codeigniter.com/, http://cakephp.org/). While it isn't particularly difficult to write your own once you have a good feel for the MVC pattern, as a beginner it will probably help you learn to use one as a guide rail.

Most of this information and some other associated common pitfalls are described in my post at http://dynamicdrive.com/forums/showpost.php?p=158839.

Dennis_Gull
09-03-2008, 05:52 PM
Thanks for the great inputs, I will start by taking a look at some of the pre-written frameworks for MVC :)

Dennis_Gull
09-04-2008, 02:47 PM
include() (or, usually preferably, require_once()) are of course still useful, but you should not be using files with names like 'right_side.php' because this indicates that the code is strongly tied to the output, something that you want to avoid at all costs ...

Just out of curiosity what should I name the side panels? :)



Likewise, you should not be handling strings of output in your PHP code. Your output must be separated from the rest of your code as much as possible

What if I have an array with a couple of values I want to print?
Lets say I select the 10 latest posts and want to output them dynamically, wont I have to do it all in php then?

Twey
09-05-2008, 08:26 AM
Name them whatever you like — but they should be templates, not PHP files, and you'll render() them, not include() them.

-–—

jim_keller
09-10-2008, 12:55 AM
There are a variety of MVC PHP frameworks available pre-written for you (http://codeigniter.com/, http://cakephp.org/). While it isn't particularly difficult to write your own once you have a good feel for the MVC pattern, as a beginner it will probably help you learn to use one as a guide rail.


Fuse is also one of the big contenders in the PHP MVC arena: Fuse PHP MVC Framework (http://www.phpfuse.net)

:D

Twey
09-10-2008, 07:36 AM
Well, you certainly seem to be promoting it enthusiastically, and I hate to disillusion you here, but that doesn't automatically make it a 'big player' *amusement*

I'll leave it, though, since as a new framework some people might find it easier to read as an example of the basic concepts. Beware, though, that it's not nearly as featureful as the others, and you probably don't want to be using it in production.