Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions / public $...
#1

[eluser]vagabond[/eluser]
hi
there are some basic rules i don't understand - honestly, there's a lot in php/oop i don't understand Wink i hope you can help me. in this case ME is involved.

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

class Default_controller extends Controller
{
    
    public $data;
    
    function Default_controller()
    {
        parent::Controller();
        $this->load->model('my_model');
    }

    function index()
    {
        $this->data = 'some text';
        $this->my_model->add_text();
        echo $this->data;
    }
    
}

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

class My_model extends Model
{

    function Model()
    {
        parent::Model();
    }

    function add_text()
    {
        $this->data .= ' ...and some more text';
    }

}

?>

results:
Quote:some text ...and some more text
$this->data passed to my_model

another_controller (calls default_controller as module)
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Another_controller extends Controller
{
        
    function Another_controller()
    {
        parent::Controller();
    }

    function index()
    {
        echo modules::run('default_controller');
    }
    
}

?>

results:
Quote:some text
$this->data NOT passed to my_model. why is that? how can i make $this->data pass to a model when in a module? maybe it's just a bad idea?

thank you.
#2

[eluser]wiredesignz[/eluser]
Hi vagabond,

The reason $this->data is not passed to your module model is because CI uses the first controller instance from which to assign class variables to the model, regardless of where the model is loaded.

In this case that is `Another_controller` and not Default_controller.

Welcome to CI forums.
#3

[eluser]vagabond[/eluser]
Thank you wiredesignz.

That means I cannot use public class variables within modules, right? Well, I could not make it happen whereever I declare "public $data". Seems logic now, even if I do not fully understand how things work. But now I'm having a lot more questions about how I am going to make my app work.

Forget the above example, new situation, say, I want my modules only to modify a array in my normal controller instead of output a view. Does this break the purpose of the modules/CI? As I realized that it is not possible to return things from a module (because of a eval() environment? correct me), I did the following:

Code:
$serialized_data = module::run('my_module' $array); // my module echoes serialize($array);
$array = unserialize($serialized_data);

Not that elegant but it works. Maybe I missed something and there's a better way to do that or looking at those codelines makes you look like my avatar :ahhh: Let me know, thanks.

Have a nice sunday
#4

[eluser]vagabond[/eluser]
[quote author="wiredesignz" date="1224410431"]
The reason $this->data is not passed to your module model is because CI uses the first controller instance from which to assign class variables to the model, regardless of where the model is loaded.

In this case that is `Another_controller` and not Default_controller.[/quote]

ci functions like uri_string() (where $this->uri_string is declared) don't output anything in my module. is this related to the above? i'm not sure, maybe i did something else wrong - and uri_string() should usually work?

thanks for your help.
#5

[eluser]vagabond[/eluser]
[quote author="vagabond" date="1228217550"]uri_string() (where $this->uri_string is declared)[/quote]

i meant $this->uri->uri_string()... Wink




Theme © iAndrew 2016 - Forum software by © MyBB