Welcome Guest, Not a member yet? Register   Sign In
Scope of $data problems
#1

[eluser]A_funs[/eluser]
I am trying to access data passed into $data in the constructor in other methods, $data is public so why isn't this working

<?php

class Company extends CI_Controller{

public $data;

function __construct(){

parent::__construct();

$data['something'] = 'something';

}



function submit_addcompany(){

$data['test_2'] = "something else";

//HOPEFULLY $data will contain both 'something and 'something else';

}

}

}
#2

[eluser]PhilTem[/eluser]
There's a difference in

Code:
$data
// and
$this->data

which you seem to not be totally clear about.

If you want to pass data between methods you either pass them as arguments or as class-attributes.
#3

[eluser]A_funs[/eluser]
Thanks,

So can you explain the difference a bit, after reading your response I tried this and got some errors...

<?php

class test extends CI_Controller{

function __construct(){

$data->name = "Alex";

}

function index(){

echo $data->name;

}





}
#4

[eluser]A_funs[/eluser]
sorry that last didn't make sense, this is still giving errors too...

<?php

class test extends CI_Controller{

function __construct(){

$data['name'] = "Alex";

}

function index(){



echo $this->$data['name'];

}





}
#5

[eluser]A_funs[/eluser]
OK got it... it was the dollar sign..

Thanks for your help
#6

[eluser]CroNiX[/eluser]
For more info on how to do OOP in php, try checking out the manual. http://php.net/manual/en/language.oop5.php. If you don't understand OOP, you will have a harder time with CI. These are concepts you should really know before using CI to get the most of it.
#7

[eluser]A_funs[/eluser]
Thanks CroNIX,

I am getting pretty close to having what I believe to be an adequate understanding of OO principles, This one distinction between properties and variables seems to be escaping me though.

If within my object I declare a public variable like "public $testdata" am I not making that variable available to all methods?

Or is what I am really doing declaring a public property, which if I want to use across methods, I need to refer to as such with $this->testdata?

thanks again
#8

[eluser]CroNiX[/eluser]
Class variables, aka properties, are accessible only when using $this, as $this refers to the entire object, not the individual method. So you need to use $this to access individual class properties throughout the class.

Code:
class Someclass extends CI_Controller {

  public $data;  //class property declaration, only accessible by using $this->data

  public __construct()
  {
    parent::__construct();

    $this->data = 'my property data';  // populate property on construct
    echo $this->data; // 'my property data'
  }
  
  function some_function()
  {
    $data = 'test'; //this is a local variable only available to this method, some_function().

    $this->data = 'other data'; //this is setting the class property, $data

    echo $data; // "test"
    echo $this->data // "other data"
  }

  function another_function()
  {
    echo $this->data;  // 'my property data'

    echo $data;  //this is undefined in this method as it is defined in some_function
  }
}

In the above, when the class is instantiated, it sets the property, $data and echos it.
'my property data'

If you go to /someclass/some_function:
-goes through constructor first, sets property $data and echos it
-sets local variable $data, sets property $data and echoes them both
'my property data'
'test'
'other data'

If you go to /someclass/another_function
-goes through constructor first, sets property $data and echos it
-echos property $data, again
-produces error that variable $data is undefined as it's only available within some_function where it was declared as a local variable.
'my property data'
'my property data'
error, $data is undefined

Hope that helps a bit. There is a difference between class variables and local variables, and you have to access them as such.




Theme © iAndrew 2016 - Forum software by © MyBB