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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hazel1919
Forum Newbie
Posts: 9
Joined: Wed Jan 27, 2016 10:07 am

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

Post 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:
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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?
(#10850)
hazel1919
Forum Newbie
Posts: 9
Joined: Wed Jan 27, 2016 10:07 am

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

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply