Welcome Guest, Not a member yet? Register   Sign In
Can I add $data in constructor?
#6

[eluser]veliscorin[/eluser]
[quote author="Aken" date="1262397731"]You need to define those as a class variable. The way you have it now, $data only exists in the constructor function. Use:
Code:
class Example extends Controller {

    private $data = array();

    function __construct()
    {
        // Your other code here

        $this->data['mostsold'] = $mostsold;
        $this->data['newproduct'] = $newproduct;
    }

}
Then when you want to access that data in your controller's functions, use $this->data instead of $data.[/quote]

I am facing the same problem. I wish to use $data in the constructor to remove repeated code. And at the same time use $data in my functions.

I figured out 2 ways.... The first is as you suggested. The other ways is to do this first in every function:
Code:
$data = $this->data;

Is there any better way to do it? than repeat this line in all my functions?


Messages In This Thread
Can I add $data in constructor? - by El Forum - 12-31-2009, 03:58 PM
Can I add $data in constructor? - by El Forum - 12-31-2009, 04:01 PM
Can I add $data in constructor? - by El Forum - 01-01-2010, 02:02 PM
Can I add $data in constructor? - by El Forum - 01-02-2010, 03:06 AM
Can I add $data in constructor? - by El Forum - 01-02-2010, 09:03 AM
Can I add $data in constructor? - by El Forum - 01-10-2010, 11:18 AM
Can I add $data in constructor? - by El Forum - 01-16-2010, 06:17 PM



Theme © iAndrew 2016 - Forum software by © MyBB