Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all ... Do no put autoloading script in routing mechanism. You are mixing the responsibilities. You will be better off creating a separate class for this based on <a href="http://php.net/manual/en/function.spl-autoload-register.php" rel="noreferrer"><code>spl_autoload_register</code></a>.</p> <p>Neeext .. do no put complicated operations on constructor. It is makes you code somewhat untestable. Maybe you should be something like:</p> <pre><code>// you might want to replace $_GET with $_SERVER['QUERY_STRING'] later $router = new Router( $_GET['url'] ); // where 'default' is the name of fallback controller $controller_class = $router-&gt;get_controller( 'default' ); $method_name = $router-&gt;get_action( 'index' ); $model_factory = new ModelFactory( new PDO( ... ) ); $controller = new {$controller_class}( $model_factory ); $controller-&gt;{$method_name}(); </code></pre> <p>Additionally, you should look into php <a href="http://php.net/manual/en/language.namespaces.php" rel="noreferrer">namespaces</a>. There is no point in ending class with <code>...Controller</code> just to know where the class will be located.</p> <p>Ok ... back to the Model.</p> <p>There is quite common misconception about models in web development community ( i blame RoR for this mess ). <strong>Model in MVC is not a class</strong>, but an application layer which contains multitude of instances. Most of the instances belong to one of two types of classes. With following responsibilities:</p> <ul> <li><p><strong>Domain Logic</strong> :</p> <p>Deals with all the computation, calculation and all the domain specific details. Objects in this group have no knowledge of where and how data is actually stored. They only manipulate the information.</p></li> <li><p><strong>Data Access</strong></p> <p>Usually made of objects that fit <a href="http://martinfowler.com/eaaCatalog/dataMapper.html" rel="noreferrer">DataMapper</a> pattern (do not confuse with ORM of same name .. nothing in common). Responsible for storing data from Domain Objects and retrieving them. Might be in database.. might not. This is where your SQL queries would be.</p></li> </ul> <p>In semi-real world situation () it might looks something like this (related to code abowe):</p> <pre><code>class SomeController { // ... snip ... protected $model_factory = null; // ... snip ... public function __construct( ModelFactory $factory ) { $this-&gt;model_factory = $factory; } // ... snip ... public function action_foobar() { $user = $this-&gt;model_factory-&gt;build_object( 'User' ); $mapper = $this-&gt;model_factory-&gt;build_mapper( 'User' ); $user-&gt;set_id(42); $mapper-&gt;fetch($user); if ( $user-&gt;hasWarning() ) { $user-&gt;set_status( 'locked' ); } $mapper-&gt;store( $user ); } // ... snip ... } </code></pre> <p>As you see, there is no indication how the data was stored. It does not even matter if user account was new, or already existing. </p> <p>Some materials you might find useful</p> <p>Videos</p> <ul> <li><a href="https://vimeo.com/21173483" rel="noreferrer">Advanced OO Patterns</a> <a href="http://qafoo.com/talks/11_11_osidays_advanced_oo_patters.pdf" rel="noreferrer">(slides)</a></li> <li><a href="http://www.youtube.com/watch?v=RlfLCWKxHJ0" rel="noreferrer">Clean Code Talks: Don't Look For Things!</a></li> <li><a href="http://www.youtube.com/watch?v=wEhu57pih5w" rel="noreferrer">Clean Code Talks: Unit Testing</a></li> <li><a href="http://www.youtube.com/watch?v=-FRm3VPhseI" rel="noreferrer">Clean Code Talks: Global State and Singletons</a></li> </ul> <p>Books:</p> <ul> <li><a href="http://rads.stackoverflow.com/amzn/click/0470872497" rel="noreferrer">Real-World Solutions for Developing High-Quality PHP Frameworks and Applications</a></li> <li><a href="http://rads.stackoverflow.com/amzn/click/0321127420" rel="noreferrer">Patterns of enterprise application architecture</a></li> <li><a href="http://rads.stackoverflow.com/amzn/click/0132350882" rel="noreferrer">Clean Code: A Handbook of Agile Software Craftsmanship</a></li> <li><a href="http://rads.stackoverflow.com/amzn/click/1934356557" rel="noreferrer">SQL Antipatterns: Avoiding the Pitfalls of Database Programming</a></li> </ul>
 

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