Welcome Guest, Not a member yet? Register   Sign In
New with Code Igniter - Application structure
#20

[eluser]TheFuzzy0ne[/eluser]
It's a question of scope. $data is available within the confines of the function it's declared in, if you declare $data as a class variable, it's available to the whole class, and accessed via $this->data. $this->something means you want to access a variable that's in the global scope of the current object. $this won't work outside of an object.

Code:
class Example {

    var $data = 'Class data';

    function getData($from_class=FALSE)
    {
        $data = "Function data";        

        if ($from_class === TRUE)
        {
            return $this->data; # returns the class variable
        }
        else
        {
            return $data; # returns the function variable.
        }
    }
}

$example = new Example();

echo $example->getData() . "\n"; # Echoes "Function data"
echo $example->getData(TRUE) . "\n"; # Echoes "Class data"
The above code is untested.

I hope this helps without causing more confusion.


Messages In This Thread
New with Code Igniter - Application structure - by El Forum - 02-25-2009, 12:42 PM
New with Code Igniter - Application structure - by El Forum - 02-25-2009, 06:32 PM
New with Code Igniter - Application structure - by El Forum - 02-25-2009, 08:22 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 12:54 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 01:43 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 01:53 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 01:58 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 02:00 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 02:08 PM
New with Code Igniter - Application structure - by El Forum - 02-26-2009, 04:24 PM
New with Code Igniter - Application structure - by El Forum - 03-03-2009, 01:32 PM
New with Code Igniter - Application structure - by El Forum - 03-03-2009, 04:40 PM
New with Code Igniter - Application structure - by El Forum - 03-03-2009, 11:48 PM
New with Code Igniter - Application structure - by El Forum - 03-05-2009, 02:42 AM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 03:43 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 05:35 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 07:12 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 07:39 PM
New with Code Igniter - Application structure - by El Forum - 04-03-2009, 08:00 PM
New with Code Igniter - Application structure - by El Forum - 04-04-2009, 04:04 AM
New with Code Igniter - Application structure - by El Forum - 06-13-2009, 04:44 AM
New with Code Igniter - Application structure - by El Forum - 06-13-2009, 05:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB