2011-06-17 09:37

$ this-> var或$ var

  • codeigniter
  • variables
  • php

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


$this -> agree = $this -> input -> post( 'agree' );

Both will work fine like:

if ($agree) { }


if ($this -> agree){ }


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douzen1896 douzen1896 10年前

    It's a matter of scope

        class Example extends CI_Controller
            private $agree1;
            public function __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
                // call the helper function
            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
    点赞 评论 复制链接分享
  • douyan2002 douyan2002 10年前

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

    点赞 评论 复制链接分享
  • dongpo2002 dongpo2002 10年前

    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.

    点赞 评论 复制链接分享
  • duanqiao9541 duanqiao9541 10年前

    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.

    点赞 评论 复制链接分享
  • doucai1901 doucai1901 10年前

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

    点赞 评论 复制链接分享
  • douren0558 douren0558 10年前

    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.

    点赞 评论 复制链接分享
  • dongtuo7364 dongtuo7364 10年前

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


    点赞 评论 复制链接分享