I want to declare a private variable inside class which I want to access in all of my class without passing it to another function using parameter. I have following scenario
class One extends CI_Controller {
private $name;
public function index () {
$name = $this->input->post('name');
$this->validate();
}
public function validate() {
echo $name;
}
}
But my above $name variable gives me an error saying variable $name is undefined.
Is above possible in PHP if yes then how?
Just using
$nameis a local variable in the scope of the function only.If you use
$this->name, you can access the private property of your controller.See PropertiesDocs.