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

  • SEARCH
  • Home
  • 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 7504079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:26:57+00:00 2026-05-29T21:26:57+00:00

I have an abstract base Controller class and all action controllers are derived from

  • 0

I have an abstract base Controller class and all action controllers are derived from it.

Base Controller class at construction initializes View object. This View object is used by all action controllers. Each action controller have different dependencies (this is solved by using DI container).

The problem is that base Controller class also needs some dependencies (or parameters),
for example, path to view folder. And the question is – where and how to pass parameters to base Controller class?

$dic = new Dic();

// Register core objects: request, response, config, db, ...

class View
{
    // Getters and setters
    // Render method
}

abstract class Controller
{
    private $view;

    public function __construct()
    {
        $this->view = new View;

        // FIXME: How / from where to get view path?
        // $this->view->setPath();
    }

    public function getView()
    {
        return $this->view;
    }
}

class Foo_Controller extends Controller
{
    private $db;

    public function __construct(Db $db)
    {
        $this->db = $db;
    }

    public function barAction()
    {
        $this->getView()->some_var = 'test';
    }
}

require_once 'controllers/Foo_Controller.php';

// Creates object with dependencies which are required in __construct()
$ctrl = $dic->create('Foo_Controller');

$ctrl->barAction();
  • 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-29T21:27:00+00:00Added an answer on May 29, 2026 at 9:27 pm

    This is just a basic example. Why is the $view private? Is there a good reason?

    class View {
      protected $path;
      protected $data = array();
    
      function setPath($path = 'standard path') {
        $this->path = $path;
      }
    
      function __set($key, $value) {
        $this->data[$key] = $value;
      }
    
      function __get($key) {
        if(array_key_exists($key, $this->data)) {
          return $this->data[$key];
        }
      }
    }
    
    abstract class Controller {
        private $view;
    
        public function __construct($path)
        {
           $this->view = new View;
    
           $this->view->setPath($path);
        }
    
        public function getView()
        {
            return $this->view;
        }
    }
    
    class Foo_Controller extends Controller {
        private $db;
    
    
        public function __construct(Db $db, $path)
        {
            // call the parent constructor.
            parent::__construct($path);
            $this->db = $db;
        }
    
        public function barAction()
        {
            $this->getView()->some_var = 'test';
        }
    
        public function getAction() {
          return $this->getView()->some_var;
        }
    }
    
    class DB {
    
    }
    
    $con = new DB;
    $ctrl = new Foo_Controller($con, 'main');
    
    $ctrl->barAction();
    print $ctrl->getAction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a abstract controller class that I all my controllers inherit from.
I have a base controller abstract class, which my other controllers inherit. This class
I have a base controller class from which my other controllers are inherited public
I have an abstract base class from which many classes are derived. I want
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract base class called Shape from which both Circle and Rectangle
I have a abstract base class A and a set of 10 derived classes.
I have an abstract base class and two derived classes. The base class contains
I have an abstract base class that I'm referencing from one of my entities.
Imagine I have abstract base class Shape , with derived classes Circle and Rectangle

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.