Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no direct answer to your question, but there is some food for thought.</p> <p><strong>Seperation of concerns</strong> You can think about if you can perhaps seperate your buisness logic and layout logic. Often the use of a template engine can help greatly with that. I've had positive experiences with for example Twig or Smarty (was some time ago, not sure how they measure up right now). It requires you to write your code in a (less linear) way, but more logical.</p> <p>A typical example of an OOP like seperation of concerns might be something like this:</p> <pre><code>$this-&gt;setParam('Myparam','myvalue'); if ($this-&gt;isAjax()) { $this-&gt;setTemplate('ajax.php'); $this-&gt;setLayout(false); } else { $this-&gt;setTemplate('normal.php'); $this-&gt;setLayout('Mylayout'); } return $this-&gt;render(); </code></pre> <p>It is an imaginative situation, which can be found in many MVC like applications and frameworks. The main idea is that you should have the possibility to seperate your layout from your data. I would suggest looking at some of the modern frameworks for inspiration (like symfony, codeigniter, zend framework).</p> <p><strong>Glossary / Often applied concepts in a decoupled PHP application</strong> Here is a quick list of concepts that can be used. </p> <p><strong>Example mvc in php:</strong> <a href="http://www.phpro.org/tutorials/Model-View-Controller-MVC.html" rel="nofollow noreferrer">http://www.phpro.org/tutorials/Model-View-Controller-MVC.html</a></p> <p>Note: I don't really like the implementation. I much more prefer the existing frameworks. I do like the explanation in total of this tutorial. E.g. for me this link is for learning, not for implementing.</p> <p><strong>Silex</strong> For a simple decoupled php micro-framework I would really recommend silex, by the makes of symfony2. It's easy to implement, and to learn, but contains mainy of the concepts described here; and uses all the php 5.3+ goodies such as namespacing and closures.</p> <p>see: <a href="http://silex.sensiolabs.org/" rel="nofollow noreferrer">http://silex.sensiolabs.org/</a></p> <p><strong>Frontcontroller Pattern</strong> Only have one, and one only point of entry for your code. I usually only have one, and one only point in your application. Usually a frontcontroller 'dispatches' the request to the rest of the application</p> <p><a href="http://en.wikipedia.org/wiki/Front_Controller_pattern" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Front_Controller_pattern</a></p> <p><strong>Routing</strong></p> <p>A routing system is often used in combination with the frontcontroller pattern. It basically describes which URL is connected to which module / controller. This allows you to change the way people access your app without changing the urls.</p> <p>See: <a href="https://stackoverflow.com/questions/115629/simplest-php-routing-framework">https://stackoverflow.com/questions/115629/simplest-php-routing-framework</a></p> <p><strong>Controller</strong></p> <p>A controller is the place where buisness logic is applied. Getting the data from the database, checking privileges, setting the template, setting the layout, etc. (although this is also moved outside the controller if it becomes too big of a seperate concern). </p> <p><strong>Model</strong> The model basically is the layer in which use manage your database. This can be a simple class where you move all your mysql_* functions, or it can be a full-featured ORM. The main philosphy is that all the logic related to fetching and placing information in the database is seperated.</p> <p><strong>One step up: ORM</strong> An often used method in applications are Object Relational Models, these 'map' SQL records to PHP objects. Doctrine and Propel are two of these well worked out libraries. I heavily rely on these systems in my development. In this sense, the doctrine or propel part will represent the model layer.</p> <ul> <li>Doctrine: <a href="http://www.doctrine-project.org/" rel="nofollow noreferrer">http://www.doctrine-project.org/</a></li> <li>Propel: <a href="http://www.propelorm.org/" rel="nofollow noreferrer">http://www.propelorm.org/</a></li> <li>Other ORMS: <a href="https://stackoverflow.com/questions/108699/good-php-orm-library">Good PHP ORM Library?</a></li> <li><a href="https://stackoverflow.com/questions/2062473/php-orms-doctrine-vs-propel">PHP ORMs: Doctrine vs. Propel</a></li> </ul> <p><strong>View:</strong> The view usually consists of a templating engine. Some use plain PHP as a template, others, such as symfony create a seperate scope in which variables are placed. There are many discussions and opinions about what is best, one is right here on stackoverflow:</p> <ul> <li><a href="https://stackoverflow.com/questions/436014/why-should-i-use-templating-system-in-php">Why should I use templating system in PHP?</a></li> <li><a href="https://stackoverflow.com/questions/731743/php-vs-template-engine">PHP vs template engine</a></li> </ul> <p>Ones I like: - Twig: <a href="http://twig.sensiolabs.org/" rel="nofollow noreferrer">http://twig.sensiolabs.org/</a> - sfTemplate: <a href="http://components.symfony-project.org/templating/" rel="nofollow noreferrer">http://components.symfony-project.org/templating/</a> - Smarty: <a href="http://components.symfony-project.org/templating/" rel="nofollow noreferrer">http://components.symfony-project.org/templating/</a></p> <p><strong>Decoupling mechanisms:</strong></p> <ul> <li><p>Event based systems Using events in your can help to seperate the code. For example if you want to send an email after a record has been saved, events are a good solution to do that; in general the model should not have to know about email. Thus events are a way to connect them: you can let your -email-send-class listen to certain records in order for them to send the right email. (Perhaps you'd rather want your e-mails send from your controller, this is probably a matter of taste).</p></li> <li><p>Dependency injection When using OOP code in PHP many relied on having singleton classes running around (configuration, etc). From an OOP point of view, this can be considered bad, because it's hard to test it, and it's not considered very elegant to have dependencies running around like that. Dependency Injection is a pattern that came form Java and is now used in the newer frameworks to get around this. It might be a bit difficult to wrap your head around, but you will see it coming back in several new frameworks.</p></li> </ul> <p>Dependency injection in php: <a href="https://stackoverflow.com/questions/4488269/dependency-injection-in-php-5-3">Dependency Injection in PHP 5.3</a></p> <p><strong>Frameworks:</strong></p> <p>A lot of these methods are difficult, or a lot of work to implement yourself. Many will reside to a framework for this. You may or may not need a framework. You may, or may not want to you a framework, it's your choice. But it's still useful to learn how the frameworks do it, and not try to reinvent the wheel yourself. </p> <p>The no-framework php frameworks: <a href="https://stackoverflow.com/questions/694929/whats-your-no-framework-php-framework">https://stackoverflow.com/questions/694929/whats-your-no-framework-php-framework</a></p> <p>Good habits: <a href="https://stackoverflow.com/questions/694246/how-is-php-done-the-right-way">https://stackoverflow.com/questions/694246/how-is-php-done-the-right-way</a></p> <p>Frameworks worth looking at (imho): CodeIgniter, Kahona, CakePHP, Symfony (1.4/2.0), Silex, Zend Franework, Yii. There are many many more, each with their dedicated fans and haters. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload