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 8224627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:08:07+00:00 2026-06-07T15:08:07+00:00

My Views expect certain variables to be defined by the controllers invoking them. As

  • 0

My Views expect certain variables to be defined by the controllers invoking them. As such, I created my own controller to extend from. This controller, in turn, extends CI_Controller.

class MainController extends CI_Controller{

static protected $data_bundle;

/**
All classes that will extend MainController should have their own constructors
calling the constructor of MainController.
*/
public function __construct(){
    parent::__construct();
    $data_bundle["title"] = "";
    $data_bundle["content"] = "";
    $data_bundle["stylesheets"] = array();
    $data_bundle["scripts"] = array();
    $data_bundle["echo_content"] = true;
}

}

So, for instance, I may define a Controller for a plain page as follows

class PlainPage extends MainController{
    public function __construct(){
        parent::__construct()
    }

    public function page(){
        parent::$data_bundle["title"] = "Plain Page"
        parent::$data_bundle["content"] = "The quick brown fox jumps over the lazy dog."
        $this->load->view("mainview", parent::$data_bundle);
    }
}

Then, in mainview.php, I have the following bit of code:

<?php
        foreach($stylesheets as $style){
            echo '<link rel="stylesheet" type="text/css" href="css/$style" />"';
        }
    ?>

    <?php
        foreach($scripts as $script){
            echo '<script type="text/javascript" src="scripts/$script"></script>"';
        }
    ?>

But I get the following errors:

Message: Undefined variable: {stylesheets|scripts}
Message: Invalid argument supplied for foreach()

Isn’t it that when parent::__construct() gets called, the $data_bundle array should’ve been initialized? Why is CI/PHP complaining that I have variables undefined?

  • 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-06-07T15:08:09+00:00Added an answer on June 7, 2026 at 3:08 pm

    You missed self in parent class constructor when initializing the static property.

    public function __construct(){
        parent::__construct();
        self::$data_bundle["title"] = "";
        self::$data_bundle["content"] = "";
        self::$data_bundle["stylesheets"] = array();
        self::$data_bundle["scripts"] = array();
        self::$data_bundle["echo_content"] = true;
    }
    

    But You don’t need to use static property in this case, controller should have only one instance, so a instance property is enough.

    class MainController extends CI_Controller{
    
    protected $data_bundle = array();
    
    /**
    All classes that will extend MainController should have their own constructors
    calling the constructor of MainController.
    */
    public function __construct(){
        parent::__construct();
        $this->data_bundle["title"] = "";
        $this->data_bundle["content"] = "";
        $this->data_bundle["stylesheets"] = array();
        $this->data_bundle["scripts"] = array();
        $this->data_bundle["echo_content"] = true;
    }
    
    }
    
    class PlainPage extends MainController{
        public function __construct(){
            parent::__construct()
        }
    
        public function page(){
            $this->data_bundle["title"] = "Plain Page"
            $this->data_bundle["content"] = "The quick brown fox jumps over the lazy dog."
            $this->load->view("mainview", $this->data_bundle);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make certain output from views dynamic based on the type of
my views.py file code: #!/usr/bin/python from django.template import loader, RequestContext from django.http import HttpResponse
DDMS views Database only in eclipse. I want to view table contents from CMD.
I have a problem where I can successfully push a new view controller from
I'm building a part of a system where the user can define views from
I have a UIViewController which supports all interface Orientations. The views are created programatically
Rails has a feature where models, controllers, views, libraries, etc. are automatically loaded when
Im having some trouble getting Ajax.ActionLink to work how I expect. In /views/layout/_Layout.cshtml I
Here is the relevant code from views/products/edit.html.erb : <%= form_for(:product, :url => {:action =>
This question is about iOS device rotation and multiple controlled views in a UINavigationController.

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.