开发者

$this->var or $var

开发者 https://www.devze.com 2023-03-13 11:41 出处:网络
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.

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 = $t开发者_开发问答his -> input -> post( 'agree' );

Both will work fine like:

if ($agree) { }

OR

if ($this -> agree){ }

Thanks


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);
        }
    }
?>


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.


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.


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.


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');


$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.


I think only $this->agree works, however, I have not tested this.

Ad@m

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号