Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1086615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:50:07+00:00 2026-05-16T22:50:07+00:00

All, I am writing a little dynamic site in php, and am trying to

  • 0

All,

I am writing a little dynamic site in php, and am trying to use dependency injection rather than globals.

In my index, I create a $view object which contains the various elements to be displayed in the UI. In the index, I also have a switch($action){} structure that governs which controllers to require based on which item the user clicks on a menu (and therefore the value of $action). I then say something like $view=execute($view) on the controller. Each execute() function (1 per controller) ends with return $view;. The idea is that I can keep in the index a $view object which contains the accumulated info generated during a user’s session.

My question is: do I need to make $view a class variable of each controller? I just want to pass it via the execute() function, modify it within the function, and return it, modified. I’m not sure whether this is still considered dependency injection, or what are the advantages of making it or not a class variable of the classes to which the object is passed.

Thanks,

JDelage

PS: A bit more code to help with understanding:

In Index.php:

require_once("Class_view.php"); // J'ai cree une classe separee pour la vue.
$view=new $view;
$view->addToViewArray("title", "projet JDelage");
(...)
switch($action){  // Creates instances of the appropriate controller under the name $command
(...)
}
$view=$command->execute($view) // Each execute() function of each controller takes $view as a parameter, modifies it, and returns it.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:50:08+00:00Added an answer on May 16, 2026 at 10:50 pm

    do I need to make $view a class variable of each controller? I just want to pass it via the execute() function, modify it within the function, and return it, modified.

    No, you do not have to make it a class/instance variable if it is only processed. It does not belong to the internal state of the controller in that case, which is what class/instance variable are for.

    I’m not sure whether this is still considered dependency injection, or what are the advantages of making it or not a class variable of the classes to which the object is passed.

    Yes, it is still considered Dependency Injection. DI is bloody simple. All you have to do is to pass dependencies into an object. How the dependency is processed inside the object is not important. You will have no advantage saving the object in the other object if you ain’t gonna need it. In fact, it’s a disadvantage, because it’s adding bloat then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a newbie with a little experience writing in BASIC, Python and, of all
I am little confused... Is it true that reading\writing from several threads all except
All, I'm writing a native application for Android and i'm stuck trying to acquire
Just writing a little function here and need some optimisation help! All requests redirect
I'm currently writing a little zsh function that checks all of my git repositories
Hi I'm having some problem with a PHP page: I'm writing a little CMS
I'm writing a little Java application that I want to use to search RSS
I'm writing a little bit of a wiki and going through all of my
I'm writing a little test program where I can look at all the notifications
Hey all, I'm writing a little 3D engine for android to better learn the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.