0

I have an object I've made

class SalesReportObject
{
    public $eventname = "";
    public $v_firstname = "";
    public $v_secondname = "";
    public $soldStock = array();
    function Push($productname, $soldquantity)
    {
        $soldStock[$productname] = $soldquantity;
    }
    function Sum($productname, $soldquantity)
    {
        $soldStock[$productname] += $soldquantity;
    }
    function __construct($e, $f, $s)
    {
        $eventname = $e;
        $v_firstname = $f;
        $v_secondname = $s;
    }

}

And I'm populating it using the following

$salesreportq = 'SELECT Volunteers.SecondName, Volunteers.FirstName, Events.EventName, ItemIssue.Quantity, ItemIssue.ReturnQuantity, Stock.ProductName, EventInstance.InstanceID 
             FROM EventInstance
             INNER JOIN Events ON EventInstance.EventID = Events.EventID
             INNER JOIN ItemIssue ON EventInstance.InstanceID = ItemIssue.InstanceID 
             INNER JOIN Volunteers ON EventInstance.VolunteerID = Volunteers.VolunteerID 
             INNER JOIN Stock ON ItemIssue.StockID = Stock.StockID
             WHERE (ItemIssue.Returned = ?)';
$yes = array('Yes');                 
$salesreportres = sqlsrv_query($conn, $salesreportq, $yes);
$arr = array();

while ($sRow = sqlsrv_fetch_array($salesreportres, SQLSRV_FETCH_ASSOC))
{
    $sold = $sRow['Quantity'] - $sRow['ReturnQuantity'];

    if (array_key_exists($sRow['InstanceID'], $arr))
    {

        if (array_key_exists($arr[$sRow['InstanceID']]->$soldStock['ProductName']))
        {
            $arr['InstanceID']->Sum($sRow['InstanceID'], $sold);
        }
        else
        {
            $arr['InstanceID']->Push($sRow['InstanceID'], $sold);
        }
    }
    else
    {   
        $arr['InstanceID'] = new SalesReportObject($sRow['EventName'], $sRow['FirstName'], $sRow['SecondName']);
        $arr['InstanceID']->Push($sRow['InstanceID'], $sold);
    }

}

Which seems to mostly work, when I print_r() it I get

Array ( [InstanceID] => SalesReportObject Object ( [eventname] => [v_firstname] => [v_secondname] => [soldStock] => Array ( ) ) )

As you can see it has filled it with variable names of the object instead of the intended values. It's probably something really dumb, but I can't see it (been at this for a while today)

My question is why is this happening and what do I need to amend? Thanks in advance for your advice

1 Answer 1

4

You need to reference your class properties with $this. Replace your class code with this:

class SalesReportObject
{
    public $eventname = "";
    public $v_firstname = "";
    public $v_secondname = "";
    public $soldStock = array();

    function Push($productname, $soldquantity)
    {
        $this->soldStock[$productname] = $soldquantity;

    }

    function Sum($productname, $soldquantity)
    {
        $this->soldStock[$productname] += $soldquantity;
    }

    function __construct($e, $f, $s)
    {
        $this->eventname = $e;
        $this->v_firstname = $f;
        $this->v_secondname = $s;
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

I'm now getting Notice: Undefined variable: eventname in C:\RDEUsers\NET\470585\reports.php on line 38 Fatal error: Cannot access empty property in C:\RDEUsers\NET\470585\reports.php on line 38
What's in the line 38?
$this->$eventname = $e;
Take a second look at my posted code, eventname isn't preceded by a $ symbol. Actually properties aren't preceded by a $ symbol when they're accessed by this.
That's because those are the properties of the class. What's your expected output? Please edit your post mentioning what you're expecting to get.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.