Welcome Guest, Not a member yet? Register   Sign In
Get variables created in MY_Controller?
#1

[eluser]chefnelone[/eluser]
hello

I created MY_Controller and defined some variable in it. Now, I don't know how to recover these variables from another controller which is extended to MY_Controller.

MY_Controller.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class  MY_Controller extends Controller{

    function __construct(){  
      parent::__construct();
      define('COMPANY_EMAIL','[email protected]');
      $this->data['company_email']='[email protected]';
    }

}
another_controller.php

Code:
<?php  if (! defined('BASEPATH')) exit('No direct script access');

class Another_controller extends MY_Controller {
   function index(){
      $data['company_email']= //what's in here to get the value of 'company_email' defined in MY_Controller.php
      $this->load->view('index', $data);
   }
}
#2

[eluser]n0xie[/eluser]
Make them class properties:

Code:
class  MY_Controller extends Controller{

  protected $company_email = '[email protected]';

Then in your extended class:

Code:
function index()
{
  echo $this->company_email;
}

Although in this case where you have to deal with 'global config' settings, you might be better off putting it in the config array or load it from the database.
#3

[eluser]chefnelone[/eluser]
[quote author="n0xie" date="1268178777"]Make them class properties:

Code:
class  MY_Controller extends Controller{

  protected $company_email = '[email protected]';

Then in your extended class:

Code:
function index()
{
  echo $this->company_email;
}

Although in this case where you have to deal with 'global config' settings, you might be better off putting it in the config array or load it from the database.[/quote]

It did't work. I get a blank page. If I remove the word 'protected' it runs fine, but I can't get the value of $company_email though;

Code:
class  MY_Controller extends Controller{

  $company_email = '[email protected]';//I remove 'protected' word to avoid blank page

extended class:

Code:
function index()
{
  $company_email = $this->company_email;
  $this->firephp->log( $company_email ); //this is line 278
}

then I get this error message:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: index::$company_email

Filename: controllers/index.php

Line Number: 278
#4

[eluser]InsiteFX[/eluser]
Your constructor is wrong CodeIgniter's core is PHP4
When you extend the MY_Controller then you can use PHP5 constructor.

Code:
class  MY_Controller extends Controller{

    function __construct(){  
      parent::__construct(); <--- WRONG! should be parent::Controller();
      define('COMPANY_EMAIL','[email protected]');
      $this->data['company_email']='[email protected]';
    }

}

Enjoy
InsiteFX
#5

[eluser]chefnelone[/eluser]
ok, I think the constructor is ok now?
How do I get the value of $company_email from others controllers?

this is still not working

Code:
function index()
{
  $company_email = $this->company_email;
  $this->firephp->log( $company_email ); //this is line 278
}
gives me the error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: index::$company_email

Filename: controllers/index.php

Line Number: 278
#6

[eluser]n0xie[/eluser]
If you use PHP4 try this:
Code:
class  MY_Controller extends Controller{

  var $company_email = '[email protected]';
#7

[eluser]chefnelone[/eluser]
I get the same error message:
Message: Undefined variable: company_email
#8

[eluser]n0xie[/eluser]
Show us your entire code. I can't guess what's going wrong.
#9

[eluser]chefnelone[/eluser]
I get this error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: company_email

Filename: controllers/mycontroller.php

Line Number: 5

if I run this code:

application/libraries/MY_Controller.php
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    class  MY_Controller extends Controller{
    
        function __construct(){
        
        parent::Controller();

        
          $company_email = '[email protected]';
        
        }

}

application/controller/mycontroller.php
Code:
&lt;?php
class Mycontroller extends MY_Controller {

    function index(){
        $data['company_email'] = $company_email; //this is line 5
        $this->load->view('myview', $data);
    }
    

}

application/views/myview.php
Code:
&lt;?php echo $company_email; ?&gt;
#10

[eluser]n0xie[/eluser]
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

    class  MY_Controller extends Controller{

        //php5
        protected $company_email = '[email protected]';    
        //php4
        var $company_email = '[email protected]';    

        function __construct(){
        
        parent::Controller();
        }

}




Theme © iAndrew 2016 - Forum software by © MyBB