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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:52:39+00:00 2026-06-05T09:52:39+00:00

I want to declare a variable in a php class which can be used

  • 0

I want to declare a variable in a php class which can be used in all its functions. I am using the codeigniter framework & to have to pass data like base_url etc in each of the functions. Also I need all of such data in a single variable. For example $data['base_url']=base_url(), $data['title']="My_Site_title" etc & then send only a single variable $data (this is a common thing in codeigniter).

As per this question, I modified my code to:

class Search extends CI_Controller {

function Search() {
    parent::__construct();
    $this->load->model('student_model');

    $this->load->helper('form');
    $this->load->helper('url');
    $this->output->enable_profiler(TRUE);

    $this->data['base_url']=base_url();  //gives an error here
 }

 function index() {
     $this->load->view('search_view', $this->data);
 }
 }

But still it gives an error at the marked place saying Undefined variable: data and Fatal error: Cannot access empty property in ..\search.php on line 16.

  • 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-05T09:52:41+00:00Added an answer on June 5, 2026 at 9:52 am

    Here you go:

    class Search extends CI_Controller {
    
    protected $data = array();
    
    function Search() {
        parent::__construct();
        $this->load->model('student_model');
    
        $this->load->helper('form');
        $this->load->helper('url');
        $this->output->enable_profiler(TRUE);
    
        $this->data['base_url']=base_url();
     }
    
     function index() {
         $this->load->view('search_view', $this->data);
     }
     }
    

    More about class properties in php: http://php.net/manual/en/language.oop5.properties.php

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

Sidebar

Related Questions

I want to declare a global variable using PHP and be used inside functions.
I want to declare a variable which holds a class which implements a specific
I want to declare a variable in such a way that I can access
I have a variable $className which is name of declared class in php and
I want to declare static(or not static) variable inside Enum. I need this because
I want to use that i declare local variable like a as clause ,
Let's say I declare a variable: String a = test; And I want to
I don't want to declare those variables in Action class again Employee POJO: package
I can: declare @idOrder int set @idOrder = 21319 I want: declare @idOrder int
here is my sample class to why i want to nest. include(class.db.php); class Cart

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.