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条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法