Welcome Guest, Not a member yet? Register   Sign In
Accessing Object Info From Function
#1

[eluser]Krumpet[/eluser]
I have a search class as shown below. In it, I want the assets function to have access to the $params class variable. Is this possible? If so, how do I accomplish this? FYI, I've greatly simplified the code below so we can focus on the question at hand.

Code:
class Search extends CI_controller{
    
    public $params;
    
    public function index()
    {
       $this->params['file_name'] = $_GET['username'] . '/' . $_GET['locale'] . '/' . $_GET['feed'];
    }
    
    public function assets()
    {
       print_r($this->params);
    }
}

As a side note, I have some suspicions that calling the assets function (http://www.blah.com/search/assets/) creates a new Search object and because there is no __construct function, the new object is 'empty' and therefor cannot access $this->params.

What I'm thinking I really need is a __construct that sets up necessary class variables. However, this seems like poor design to me. I don't want to create two objects that contain the same info.

I know a decent amount about PHP, but my experience with OOP code design is limited so thinking through situations like this is tough...thanks in advance.
#2

[eluser]theprodigy[/eluser]
The point of constructors (__construct for php) is to run code on object instantiation that needs to be run, and this includes setting default values for class level variables that need to have values.

Your 'suspicions' are right in that it is creating a new object with each page load, so if you call /search/assets/ directly, your class level variable will not have a value, unless you set it in the constructor.

Setting default values for class level variables is not only NOT poor design, it's a common practice. Either that, or set the variable at the declaration*

Code:
class Search extends CI_controller{
    
    public $params = 'default value';
    
    public function index()
    {
       $this->params['file_name'] = $_GET['username'] . '/' . $_GET['locale'] . '/' . $_GET['feed'];
    }
    
    public function assets()
    {
       print_r($this->params);
    }
}

* I believe if you set it at declaration, it can only be set to base types (strings, integers, arrays, etc), and not function calls, other variables, etc (so you won't be able to set it to your $_GET concatenated string).
#3

[eluser]toopay[/eluser]
Also, its GOOD practice to use $this->input->get() instead $_GET like that!
#4

[eluser]Krumpet[/eluser]
Thanks! All good stuff.

However, what if the $params values I want to access in the Search class are set in the index function? Basically, I have a form controller that is passing info to the search controller. The search controller then sets the $params values. I want to get at those $params values from the assets function. How can I do this without having to reset the $params values? I'm almost certain setting those values twice is bad form...

The user flow looks like this...

http://www.this.com/form/ >> http://www.this.com/search/ >> http://www.this.com/search/assets/




Theme © iAndrew 2016 - Forum software by © MyBB