1

did can be defined a variable for multi function(in codeigniter)? how is it?

because i must use of a value similar in multi function.

like:

  class Home extends CI_Controller {
   $hi = 'hello'
    function one() {
       echo $hi;
    }
    function tow() {
       echo $hi;
    }
  }

1 Answer 1

1

what about doing something like this

class Home extends CI_Controller {
    protected $_hi = 'hi';
    function one() {
       echo $this->_hi;
    }
    function tow() {
       echo $this->_hi;
    }
  }

if the hi is a constant you have better to use the const keyword

  class Home extends CI_Controller {
    const HI = 'hi';
    function one() {
       echo self::HI;
    }
    function tow() {
       echo self::HI;
    }
  }

last point if that constant is used in more than one controller you have better to create a separate class and define the constant in that class.

  class Home extends CI_Controller {
    protected $_find;

    function __construct() {
        parent::__construct();
        $this->_find = $this->input->post('find');
    }

    function one() {
       echo $this->_find;
    }
    function tow() {
       echo $this->_find;
    }
  }

one remark with the last code snipet, I am not an codeigniter expert so not sure if you can do $this->input->post('find') would work in the constructor

Sign up to request clarification or add additional context in comments.

2 Comments

protected find = $this->input->post('find'); // Line 3 = in function => $find = $this->find; this have error: Parse error: syntax error, unexpected T_VARIABLE in D:\xampp\htdocs\mehdi\application\controllers\admin\accommodation.php on line 3 ?
Kate: you cannot set variable with output from a function ... see my edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.