array in controller not recognized in rest of class' functions? |
[eluser]ScottBruin[/eluser]
Hi, I'm having trouble with the following code. I load helpers, a library, and a model, as well as define a $rules array for CI's validation, and set the rules. When I load athlete/edit, I get an undefined error for the $rules array. Is there some sort of scope that I'm misunderstanding? It seems to me that everything on the constructor should carry into the methods. Thanks for any help. Code: class Athlete extends Controller {
[eluser]Michael Wales[/eluser]
$rules is a class variable and the following line is referring to a local variable (a variable for that function only): Code: $input = userules_forinput($rules); // **** LINE WITH UNDEFINED ERROR for $rules Change it to: Code: $input = userules_forinput($this->rules);
[eluser]ScottBruin[/eluser]
Thank you for your response, Michael. I'm now getting another error from the helper function that calls $this->rules. Here's the function: Code: function userules_forinput($rules) I won't lie, I'm not quite sure what $Ci =& get_instance() does. I saw it on this forum and it fixed my problem, but my understanding of OOP is poor.
[eluser]ScottBruin[/eluser]
I edited above. For the return of the variable $input. Says the variable is undefined.
[eluser]Pascal Kriete[/eluser]
The variable is only defined in the scope it was initialized in. In this case that means the foreach loop. Code: $input = array(); //Declare it here, so that it's scope is the whole function
[eluser]ScottBruin[/eluser]
Thanks, inparo, that partially fixes the problem. Still, I don't understand why that was not a problem before I moved the definition of the $rules array and the setting of the rules to the constructor. So, I've made the two suggested changes by you and Wales, but am getting this error now: Message: Undefined property: Athlete::$rules. I think I just have a poor grasp of OOP and how CI works, so that's my struggle. I defined the rules array in this way: $this->rules['key'] = 'value' and this seems to solve my problems after making your change and changing use of $rules to $this->rules everywhere. Is this the correct way of doing things? Is it best practice to define the array $rules and then say something like $this->rulesarray = $rules? Thanks for any help, I'm obviously confused.
[eluser]Pascal Kriete[/eluser]
Ok, I think the first thing we should clear up is the scope problem. Every variable is set in what is called a scope - most of the time it just means that it's inside curly braces. Example: Code: class Something { So now we'll get to your class: Code: class Athlete extends Controller { I hope I got all that right (it's almost 3am), and I hope it helps . EDIT: Forgot this part: There is a CodeIgniter object, that most of the time you just call using $this->whatever (load, etc). That object is only defined in the views, models, controllers. To get it in your own classes you do $CI =& get_instance(). The & means that you're getting a reference (a pointer at the object) instead of copying it. And now you can use $CI as you would $this.
[eluser]wiredesignz[/eluser]
Quote:There is a CodeIgniter object, that most of the time you just call using $this->whatever (load, etc). That object is only defined in the views, models, controllers. To get it in your own classes you do $CI =& get_instance(). The & means that you’re getting a reference (a pointer at the object) instead of copying it. And now you can use $CI as you would $this. Controllers, Models and Views are part of the CI super-object and therefore can use $this to reference the CI core, libraries are not and require the use of &get;_instance() to reference the CI core. There should be a sticky for this stuff it gets posted so often.
[eluser]ScottBruin[/eluser]
Thank you inparo and wiredesignz, this helped me a ton and should be required reading for those using CI. I agree, a sticky thread or some documentation should be more readily available about how CodeIgniter operates (though I understand some of my misunderstanding was because of poor grasp of OOP principles). This is a good article on how CI uses a super-object: http://www.packtpub.com/article/codeigniter-and-objects Also, to close up this thread for anyone that turns to it for help in the future: I found that inparo's code is slightly flawed in that declaring the variable $rules = array(); in the class needs a declaration like "var" or "public" |
Welcome Guest, Not a member yet? Register Sign In |