Welcome Guest, Not a member yet? Register   Sign In
Properties in my controller
#1

[eluser]skaterdav85[/eluser]
I'm having trouble creating a property in my controller. This is what i put above all my methods in one of my controller classes:

Code:
public $_DATA['title'] = "Contact";
However, I get this error:

Code:
Parse error: syntax error, unexpected '[', expecting ',' or ';' in /Applications/MAMP/htdocs/TIS/application/controllers/contact.php on line 4
I'm not sure if this is something specific to CI or you just cant assign an index and value to an array in the property declarations. Anyone know?
#2

[eluser]InsiteFX[/eluser]
Try this:

Code:
public $data = array();

$data['title'] = "Contact";

InsiteFX
#3

[eluser]skaterdav85[/eluser]
That did not work either. Actually, what I found out was that PHP wont create an array for you if you're using it as a property in a class. You have to explicitly create the array, and then you can assign elements to it. If you do something like $_DATA['title'] = "Contact" not in a property context, PHP will create the _DATA array for you if it has not been created. I'm not really sure why. Anyways, this solved the problem:

Code:
public $_DATA = array('title'=>'Contact');
#4

[eluser]TheIgniter[/eluser]
try to use :

Code:
$this->data = "contact" ;
#5

[eluser]skaterdav85[/eluser]
well i wouldnt want to set the data property through a method or constructor because the value never changes. it is my page title. otherwise in every method i would have to do $this->data = 'Contact', and that's just annoying.
#6

[eluser]TheIgniter[/eluser]
Then you have to do that in the constructor...
#7

[eluser]TheIgniter[/eluser]
Code:
public function __construct()
    {
        $this->data = "contact" ;
        }
          }
     public function index()
     {
        echo $this->data ;
     }
    
      public function test()
     {
        echo $this->data ;
     }




Theme © iAndrew 2016 - Forum software by © MyBB