A PHP5 MVC system



  • Hello,

    Looking to learn how to implement a simple MVC system in PHP, i came accross this tutorial :

    http://www.phpit.net/article/simple-mvc-php5/

    and although I found it very interesting as a first start, in order to understand the basic principles of MVC, I'm now wondering if this particular example really is such a good one.

    The 2nd response by "solid" does make sense (why didn't the original coder use __set and __get if he's going to use other php5 features anyway ? is the "registry" class really needed and if not, how to do without it ?)

    As an exercice, I've tried to adapt the resulting program to my needs, and I'm worried that I might be learning bad practices just because some guy told so, on a website that doesn't even seem to be updated anymore (and their forums are down).

    In short, what do you big guys think of this tutorial ? anyone knows of better tutorials that I could learn from ? 



  • Why not just use an existing framework, like Symfony or CakePHP? No need to reinvent the wheel.



  • Sure... for a commercial project or something really important, i'd agree with you, but when it comes to learning, I'm fine with reinventing the wheel... the point here was to understand the principles and concepts of MVC, and I thought learning how to build one from scratch would be interesting. Which is why i wanted to be sure that i wasn't learning from crappy material

    otherwise, I'd go with CakePHP (though I'm trying to learn python these days, so I might rather have a look at Django)

    bah, anyway. thanks for the answer.



  • I think a lot of the things being done there are exercises in overcomplication and an attempt to shoehorn SPL in places where it's not even needed.

    Starting with the registry class: First, what's so wrong about using global variables to store global data? Is it so bad to write 'global' when you need it in a function? The global namespace isn't the scary void of chaos most people seem to imagine it as. If you want, make your global variables UPPERCAPS or some other format to tell them apart from regular ones.

    Second, if I were to use a Registry class for some reason, here's my implementation. It does everything that his does, apart from the superfluous ->get() syntax:

    $registry = array();

    BAM! I licence it under the GPL. Feel free to use how you like.

    Next, Router. That's fine if you want to map your requests to class methods. I personally find it pointless because during each request, you'll only have one method being called every time. What's the point of loading the whole class of actions when you only need one of them? In any more complex example, that's just asking to become convoluted.

    Just include the php file you need from the request and pass on control to it. If you like, instantiate some kind of Page class from it. Split the processing and the rendering between functions in that class. But there's absolutely no need to follow the purist, strict-typed MVC approach. PHP's weak typing is a powerful tool that can make your job so much easier if you use it right.



  • First, what's so wrong about using global variables to store global data?
    because whoever wrote the Registry class can't be sure that writing whatever variables into global scope won't overwrite other global variables.


  • And then, you turn around and make $registry a global variable anyway. 

    It's a non-issue. It should be trivial to control as long as your framework isn't running on top of another framework, and if your MVC framework is already running in another MVC framework, then you have bigger things to worry about.


Log in to reply