Page 1 of 1

(passing data around) Is this good practice for my personal

Posted: Mon Jul 04, 2016 9:26 am
by hazel1919
Hi all,

I have just put together a tentative proof of concept MVC framework proposal.

After many weeks of slogging through an array of PHP MVC tutorials and courses (some of which I paid for) I have decided to strip back all the frills, bells and whistles to try and put together a core proof of concept that uses best practices.

In this simplified example I am trying to test the viability of the framework itself... so there is no routing code or query string management and the controller and action are hard coded.

Here is the repository: https://github.com/JethroHazelhurst/psr-4-mvc

I like this framework because it:
  • Has clear seperation of concerns
  • Is not cluttered with static functions
  • Is clearly namespaced - thanks to the PSR-4 autoloading
  • Has a clear way of passing data to and from the model - e.g. $this->_view->foo = $this->_model->getBar();
Below is the directory structure:

Image

and here is a print_r of my Object structure:

Image

Questions

I am interested in hearing any feedback on the idea... I can't see any glaring issues at them moment. Some questions in the back of my mind are:
  • Are there any dangers in heavily depending on the parent::__construct() function to call parent classes?
  • In the controller, is passing data from Model to View using $this->_view->foo = $this->_model->getBar(); good practice?
  • Are there any dangers in using Namespaces?
I am also interested in reading up on MVC framework best practices so if anyone can recommend some resources I would be very grateful.

It seems like there are a million ways to write an MVC framework, I am confused as to which way is best practice.

EDIT: Hmm, can't seem to get a list to display here... :dubious:

Re: (passing data around) Is this good practice for my perso

Posted: Mon Jul 04, 2016 9:39 am
by Celauran
Looked very quickly through the code. Ignoring the hardcoded calls, since it's just a PoC. That said, why try to create your own DBAL/ORM when there are robust ones from which to choose? Leverage third-party packages when you can. Don't fall victim to Not Invented Here. Also, tell, don't ask. Inject your dependencies. Don't new up instances inside objects. It hides their dependencies and makes them difficult to test.

Re: (passing data around) Is this good practice for my perso

Posted: Tue Jul 05, 2016 7:24 pm
by Christopher
I agree on the hard coded stuff. What about Controller/Views that don't use a Model -- only show templates. What about Models that don't use a database? What about views that don't use header/footer templates but wrapper/inner templates or a hierarchy instead. The Router isn't actually a router. Where's the Front Controller?

Re: (passing data around) Is this good practice for my perso

Posted: Wed Jul 06, 2016 8:07 am
by hazel1919
Hi guys,

Really appreciate you looking through the code...

@Christopher I have a routing system that works using predefined regular expressions to match routes, it works... what I am concerned about here is the overall architecture of my framework so to make it as clear as possible I have stripped out all of the components that I know work, such as routing.

@Celauran
That said, why try to create your own DBAL/ORM when there are robust ones from which to choose?
Purely to educate myself on the topic... I don't have the skill or confidence to even make a choice, let alone implement pre-made php frameworks...
Thanks for the "Not invented here link" I saw that before and thought it was brilliant, but I think this quote sums up my position...

"Don't Reinvent The Wheel, Unless You Plan on Learning More About Wheels"

Now I with regard to your last comment (which provided the basis of my research yesterday)...
Inject your dependencies. Don't new up instances inside objects. It hides their dependencies and makes them difficult to test.
I am confused as to how I should go about this. The framework works as is, but I would like to expand upon it down the road and don't want my code quality to degrade with each new feature.

Here is a flowchart of the current framework without dependency injection...

Image

As I understand it, the main dependencies are...

Core\Router depends on Foo\Controller
FooController depends on Core\Controller (via the parent::__construct method)
Core\Controller depends on Core\View
Foo_Model depends on Core\model which depends on Core\Database

So I am a bit confused as to how I should use dependency injection here... for example: How do I implement dependency injection with parent::__constructors (if at all)?

Also, is depending on parent::__construct like this making the framework too tightly coupled?

Thanks for the time you have spent on helping me already.

Re: (passing data around) Is this good practice for my perso

Posted: Wed Jul 06, 2016 8:22 am
by Celauran
If you haven't seen it already, PHP: The Right Way has some good stuff on DI.

tl;dr: replace new in your methods with passed in parameters.

Re: (passing data around) Is this good practice for my perso

Posted: Thu Jul 07, 2016 3:27 pm
by Christopher
Typically your bootstrap code would create a DI container and then your MVC classes would get their objects from that. The bootstrap defines the initialization and hides the details from the underlying code.