I am reading a lot of PHP MVC material. There's a lot to take in at once. I am trying to create one from scratch, but it seems very difficult especially with OOP involved. Is this out of my level? If so, what steps should I take? Maybe I am trying to hard by trying to create everything perfectly right off the bat. I appreciate any reading suggestions.
You should better understand the OOP in the first place to be able to create your own MVC-based framework.
I would strongly recommend you going though this amazing tutorial at phpro.org:
Object Oriented Programming with PHP
Here are topics covered in the tutorial:
- What is OOP
- What is an Object
- What is a class
- Commenting code
- Inheritance (Extending a class)
- Visibility (public, private, protected)
- Final
- Abstract Classes
- Static Methods and properities
- Interfaces
PHP Class Functions
- get_declared_interaces()
- get_class()
- class_exists()
- get_declared_classes()
Autoload
- Serializing Objects
- Overloading
- Class Constants
Once you find yourself good at OOP, you would find that it is actually not that difficult to create your own framework from scratch. Here is a good tutorial at the same amazing site for PHP tutorials and great articles:
I recommend getting familiar with an existing MVC framework before you try to create your own.
The Zend Framework is free.
You could be overestimating MVC and thinking it actually solves problems, (as it is frequently advertised as the final solution to web programming problems) instead it's only a tiny step in the right direction.
It's ill defined too, there's:
- the model... those are the classes accessing the database,
- the view... those write the interface and sometimes are themeable.
- And the controller, literally: anything else
There really isn't much more to that than this, the real point is not to write classes and functions that take data from the db, and print html directly... Which is pretty much what everyone did in the 90s and for most of the 2000s.
The Symfony documentation has a good explanation of MVC in PHP. It works an example from procedural code up to using the framework, one layer at a time.
ZJR is right, it's simply about separating your logic (and not necessarily using the OOP paradigm). You want to decouple the logic that accesses your database (or CSV files, or whatever data source you have), from your presentation logic (the thing actually parsed by a web browser: dynamically generated HTML through PHP), from your glue that ties these two pieces together (most commonly, your form processing logic). That's it at its core.
Essentially: Don't mix your database access, your HTML generation, and your form processing in the same files. Separate this logic, put it in separate files, group those files together into three groups: MVC.
If I were you, I'd start grouping my logic (as I've outlined above). Once you've separated out your logic, start turning your model logic into OOP objects (google/stackoverflow search for things like 'object oriented php database').