Reputation: 3749
I just started using OOP / CodeIgniter. I want to get assign the form input to variables. I wonder which one I should use $this -> var
or $var
and how they differ from each other? Thanks.
For example
$agree = $this -> input -> post( 'agree' );
OR
$this -> agree = $this -> input -> post( 'agree' );
Both will work fine like:
if ($agree) { }
OR
if ($this -> agree){ }
Thanks
Upvotes: 1
Views: 3524
Reputation: 57926
If you use $this->var
then you are referring to a class variable. If you are assigning it to just $var
then you are referring to a local variable. I am gussing you need to make use of the following if you don't need the form values to be available to other methods:
$agree = $this->input->post('agree');
Upvotes: 1
Reputation: 2791
It's a matter of scope
<?php
class Example extends CI_Controller
{
private $agree1;
public function __construct()
{
parent::__construct();
}
public function index()
{
$agree2 = $this->input->post( 'agree' );
$this->agree1 = $this->input->post( 'agree' );
// within this context both are accessable
// these will print the same
var_dump($agree2);
var_dump($this->agree1);
// call the helper function
$this->helper();
}
private function helper()
{
// within this context $agree2 is not defined
// these will NOT print the same. Only the 2nd will print the content of the post
var_dump($agree2);
var_dump($this->agree1);
}
}
?>
Upvotes: 2
Reputation: 7569
$this->agree)
if you're going to use it in other functions of the class, $agree
if using it within the current scope, meaning inside the function making it a local variable only.
Upvotes: 0
Reputation: 5626
It depends on whether you want to set a local variable or an object variable.
You should have your object variable declared at the beginning of the class (e.g. private $var
) - they are accessible from different methods across the class.
Local variable is accessible only in the scope of current method.
Upvotes: 1
Reputation: 50019
I'm assuming that you're talking about what to use inside a controller/action pair?
$this->var
actually refers to a property of your controller class named var
.
$var
means that it's a locally (function) scoped variable
If you do not specifically want to access a class property, don't use $this
. Just use $var
and have it accessible only within the scope of the function.
If you are actually referring to a class property and you want this property to be acessible by all methods in your class, make sure you declare it in your class definition on top.
Upvotes: 2
Reputation: 8920
This is really a matter of preference when it comes to extra local variables. As general guidance, I would use $var
if the variable is only pertinent to the method, and $this->var
if it makes sense for other methods to use this variable too.
If you are just collecting the input and handling it in that method, I would just use a local variable. Class members are normally used for things relevant to the class/object, for example a class representing a vehicle might have a $number_of_wheels
variable.
Upvotes: 2
Reputation: 2049
I think only $this->agree
works, however, I have not tested this.
Ad@m
Upvotes: -2