Jump to content

Need help retrieving a value from an instance property


eldan88
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hey Guys. I have a shopping cart script that loops through the shopping cart session  array and assigns values to the property of a class. For some weird reason the subtotal property only works when I use the self keyword. If I use the $this-> instance variable  the sub total does not get assigned the subtoal price. Its just NULL.

 

Does anyone have any suggestions as to why it may not be working.

 

Below is a quick copy and paste of my code. Please be aware that some there may be some syntax mistakes as a result to copying and pasting part of my code that is relevant to this issue. Thanks!

class CoreCartFunctions {

protected static $subtotal;
protected $menu_item_price = NULL;

public function GetMenuItem() {

//self:items is the cart session array that is initilized in a construct method
//Assign the values for each cart array
      foreach (self::$items as $menu_item_id_session) {


$this->menu_item_price = getItems($this->menu_item_id,"menu_item_price")*$this->item_qty;

self::$subtotal += $this->menu_item_price; 
//Does not work with the following ---> $this->subtotal += $this->menu_item_price; 

}




}
Link to comment
Share on other sites

self and $this are two entirely different things.

 

self refers to the class, whereas $this refers to the current object. The objects do not have any property named “subtotal”, so there is no $this->subtotal. However, the CoreCartFunctions class has a subtotal property, so you can use self::$subtotal.

Link to comment
Share on other sites

LOL.I know the difference between  self and $this are. You don't need to teach me.

 

My problem was when i change the property

 

protected static $subtotal to protected $subtotal

 

and self::$subtotal += $this->menu_item_price; to $this->subtotal += $this->menu_item_price;

 

The subtotal doesn't get assigned!

Link to comment
Share on other sites

What you're saying simply doesn't fit together.

 

In the original post, you confused static and non-static attributes. This is a simple problem with a simple solution. You should actually have gotten an error message.

 

Now you describe an entirely different situation: You say that you understand the difference between static and non-static and still got a bug. So we're talking about different code and a different problem? Then post it. We can't help you based on code which you don't even use.

Link to comment
Share on other sites

  • Solution

if you're using the $subtotal property outside of your class you wont be able to because you have set its visibility to protected. Either set its visibility to public or create a new method which will return the value of the subtotal property

Link to comment
Share on other sites

None of this makes any sense. When you're using $this, then you're in the instance, which means visibility is irrelevant. Visibility defines the external access.

 

So whatever code you're having trouble with, it's not the one you posted above.

 

It might be a good idea to actually investigate the bug and fix it rather than abusing public visibility as a cheap workaround. But I guess you're happy with “It works” (whatever that means).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.