Welcome Guest, Not a member yet? Register   Sign In
accessing base class variable
#1

[eluser]mehwish[/eluser]
hi everyone. I am new to CI and stuck in a step. I have a base class named as MY_controller in application/library folder containing the following code:
Code:
<?php


class MY_controller extends Controller{


    
    function MY_controller()
    {
        parent::controller();
    
    }


    function MY_controller_function()
    {
               $user_name='base class variable successfully accessed';
        
    }


} //end of class

?>

and also another class Form_controller in application/controller containing the following code in which i am trying to echo the user_name variable but it is not working
Code:
class Form_Controller extends MY_Controller {
  
    function Form_controller()
    {
        parent::Controller();
                
    }


function my_profile_page($data2=null)
{ echo $this->user_name;}


} end of class

Plz help me out in solving this. I have searched alot and found the sam eway to echo the value i.e., using $this->user_name but it is showing nothing
#2

[eluser]Armchair Samurai[/eluser]
You need to define your variables with the correct scope:

Code:
class MY_Controller extends CI_Controller {

    var $user_name = 'base class variable successfully accessed';

    //Other MY_Controller stuff....

}

Also, if you're using the 2.x branch, you'll need to put MY_Controller in the Core folder, not the Library.
#3

[eluser]mehwish[/eluser]
Actually I want to set some variables global so that from any method in my Form_controller class can access the value.
Code:
<?php


class Form_Controller extends MY_Controller {
    
    function Form_controller()
    {
        parent::Controller();
         }


     function index()
    {
                  if ($this->input->post('submitbutton2'))  
                      $email1=$this->input->post('email');
                  else $this->load->view('someview');
} // end of function index


function my_profile_page()
{echo $this->email1;}

}//end of class


Now I want to print the 'email1' variable. What should be the way to print?
#4

[eluser]InsiteFX[/eluser]
Code:
class MY_controller extends Controller{

    public $user_name;
    
    function __construct()
    {
        parent::__construct();
    
    }

    function set_user_name($value)
    {
               $this->user_name = $value;
    }

} //end of class

Code:
class Form_Controller extends MY_Controller {
  
    function __construct()
    {
        parent::__construct();
    }

    function my_profile_page($data2=null)
    {
        echo $this->user_name;
    }

} end of class

InsiteFX
#5

[eluser]mehwish[/eluser]
thanx alot but one thing more i want to know that i just asked that is how can i access a variable from a method whose value is set in any other method of the same class ... for elaboration i have also posted the code ... my post#2's code. Please guide me
#6

[eluser]InsiteFX[/eluser]
the variable would need to be like the code above that I posted. If the variable is in the same class then you can make it private etc.

If you ned to keep between calls then make it like public static $var.

InsiteFX
#7

[eluser]mehwish[/eluser]
it is not working Sad
#8

[eluser]toopay[/eluser]
Put that in construct...
Code:
class MY_controller extends CI_Controller{

    protected $foo;
    
    function __construct()
    {
        parent::__construct();
        $this->foo = 'bar';
        // Or you can retrieve the $foo variable from config, or model
        // $this->foo = $this->some_model->some_function();
    }

} //end of class
Then you can access it from any child class function...
Code:
// ....
function test()
{
   $local_foo = $this->foo;
   // and so on
}
// ...
#9

[eluser]mehwish[/eluser]
this too is not working ... what is the problem .. i have tried this too
#10

[eluser]mehwish[/eluser]
leave the base class concept. Simply i want to know how can I declare a variable so that when I set the variable's value in one method .. I can access the same variable's value from any other method of the same controller's class. I have tried declaring variable private public etc. but a strange behaviour i came to see for example i declare public $vari and assign it the vaue of $email1 in index function. Then from my_profile_page()function I am able to print the value of vari using $this->vari but when i use the same statement in my my_f() function it shows nothing.



Code:
<?php

class Form_Controller extends MY_Controller {

    public $vari;
    function Form_controller()
    {
        parent::Controller();
    }


function index()
{
if ($this->input->post('submitbutton2'))
{
$email1=$this->input->post('email');
  $this->vari=$email1;
}
else $this->load->view('someview')

} // end of index




function my_profile_page($data2=null)
{
echo $this->vari;
}




function  my_f()
    {echo $this->vari;}




Theme © iAndrew 2016 - Forum software by © MyBB