dongxian3418 2011-06-17 09:37
浏览 62
已采纳

$ this-> var或$ var

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

  • 写回答

7条回答 默认 最新

  • douzen1896 2011-06-17 09:47
    关注

    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);
            }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料