Welcome Guest, Not a member yet? Register   Sign In
Problem with arrays
#3

[eluser]mddd[/eluser]
You should read up on variable scopes. A variable that you declare inside a function only exists in that function.
If you want to access it from outside the function you need to store the variable somehere else. A logical place is to make it a class property (that is, a variable that is common to all functions in the class).
Code:
<?php
class Product extends Controller
{

    var $products;

    function Product()
    {
        parent::Controller();
    }

.....

    function some_function()
    {
        // to get to the products variable, use $this->products
        $this->products = 'somevalue';
    }

    function other_function()
    {
        // you can still use the variable here, even though we're in another function
        print_r($this->products);
    }
}


Messages In This Thread
Problem with arrays - by El Forum - 07-23-2010, 12:18 AM
Problem with arrays - by El Forum - 07-23-2010, 01:13 AM
Problem with arrays - by El Forum - 07-23-2010, 01:26 AM
Problem with arrays - by El Forum - 07-23-2010, 01:33 AM
Problem with arrays - by El Forum - 07-23-2010, 01:36 AM
Problem with arrays - by El Forum - 07-23-2010, 02:01 AM
Problem with arrays - by El Forum - 07-23-2010, 02:06 AM
Problem with arrays - by El Forum - 07-23-2010, 02:23 AM
Problem with arrays - by El Forum - 07-23-2010, 02:32 AM
Problem with arrays - by El Forum - 07-23-2010, 02:45 AM
Problem with arrays - by El Forum - 07-23-2010, 03:01 AM
Problem with arrays - by El Forum - 07-23-2010, 03:03 AM
Problem with arrays - by El Forum - 07-23-2010, 03:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB