Part of the EllisLab Network
   
 
Modular Extensions / public $...
Posted: 17 October 2008 08:36 PM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  6
Joined  10-12-2008

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

<?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

<?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:

some text ...and some more text

$this->data passed to my_model

another_controller (calls default_controller as module)

<?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:

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.

 Signature 

Es gibt keine dummen Fragen, nur dumme Leute die sich nicht trauen zu fragen // There are no stupid questions, only stupid people who don’t dare to ask.

Profile
 
 
Posted: 19 October 2008 12:00 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  2634
Joined  06-10-2007

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.

 Signature 

URI Language Identifier | Modular Extensions - PHP5 | Modular Separation - PHP5 | Widget plugin | Access Control library

Profile
 
 
Posted: 19 October 2008 11:31 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Avatar
Total Posts:  6
Joined  10-12-2008

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:

$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 shock  Let me know, thanks.

Have a nice sunday

 Signature 

Es gibt keine dummen Fragen, nur dumme Leute die sich nicht trauen zu fragen // There are no stupid questions, only stupid people who don’t dare to ask.

Profile
 
 
Posted: 02 December 2008 12:32 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Avatar
Total Posts:  6
Joined  10-12-2008
wiredesignz - 19 October 2008 12:00 AM

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.

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.

 Signature 

Es gibt keine dummen Fragen, nur dumme Leute die sich nicht trauen zu fragen // There are no stupid questions, only stupid people who don’t dare to ask.

Profile
 
 
Posted: 02 December 2008 12:34 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  6
Joined  10-12-2008
vagabond - 02 December 2008 12:32 AM

uri_string() (where $this->uri_string is declared)

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

 Signature 

Es gibt keine dummen Fragen, nur dumme Leute die sich nicht trauen zu fragen // There are no stupid questions, only stupid people who don’t dare to ask.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120384 Total Logged-in Users: 22
Total Topics: 126500 Total Anonymous Users: 4
Total Replies: 665232 Total Guests: 279
Total Posts: 791732    
Members ( View Memberlist )