eldan88 Posted June 6, 2014 Share Posted June 6, 2014 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/ Share on other sites More sharing options...
Jacques1 Posted June 6, 2014 Share Posted June 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482052 Share on other sites More sharing options...
eldan88 Posted June 6, 2014 Author Share Posted June 6, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482058 Share on other sites More sharing options...
Jacques1 Posted June 6, 2014 Share Posted June 6, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482069 Share on other sites More sharing options...
Solution Ch0cu3r Posted June 6, 2014 Solution Share Posted June 6, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482072 Share on other sites More sharing options...
eldan88 Posted June 6, 2014 Author Share Posted June 6, 2014 Hey Ch0cu3r. I changed the visibilty to public and it worked. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482084 Share on other sites More sharing options...
Jacques1 Posted June 6, 2014 Share Posted June 6, 2014 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). Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482087 Share on other sites More sharing options...
Maq Posted June 6, 2014 Share Posted June 6, 2014 As Ch0cu3r mentioned, typically you would leave $subtotal private and create getter/setter methods to access it. Quote Link to comment https://forums.phpfreaks.com/topic/289013-need-help-retrieving-a-value-from-an-instance-property/#findComment-1482088 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.