eldan88 Posted May 13, 2014 Share Posted May 13, 2014 Hey Guys. I have a class named CoreCartFunctions. In the class there is a protected property named $menu_item_id which is initialized as null. A method named GetMenuItemId assigns the $menu_item_id its value. I have tested it out using the following, to see if a value got returned and it did fb($menu_item_id->GetMenuItemId(), "This is the menu id"); The problem is when I access the property from a different public method in the same class it dones't return anything. fb($menu_item_id->DisplayMenuItems()); It only returns something when I hard code a value to it.... Not sure why this is happening. Here is my full code. Please not the example below may have some syntax errors since I just copied and paste pieces of my code, to show a quick and dirty example. class CoreCartFunctions { protected $menu_item_id = NULL; public static $items; //Equal to the a long session string protected function GetMenuItemId() { foreach (self::$items as $menu_item_id_session) { /*********************************** Get the id of the menu item item ************************************/ $this->menu_item_id = preg_match_all('/\-(.*?)\*/',$menu_item_id_session,$match_found)?$match_found[1][0]:""; // The following line shows example of above line // $menu_item_id = "12"; public function DisplayMenuItems(){ return $this->menu_item_id; // Doesn't return Item ID } } Quote Link to comment https://forums.phpfreaks.com/topic/288451-not-able-to-access-my-id-property/ Share on other sites More sharing options...
trq Posted May 13, 2014 Share Posted May 13, 2014 Your code makes zero sense. Quote Link to comment https://forums.phpfreaks.com/topic/288451-not-able-to-access-my-id-property/#findComment-1479301 Share on other sites More sharing options...
Jacques1 Posted May 13, 2014 Share Posted May 13, 2014 Hi, I agree. It's probably best to rewrite this. Why on earth do you call a method “Get...” when it actually sets an attribute? Why do you keep overwriting the same attribute with a loop? Why do you use preg_match_all() when you only need a single match? Quote Link to comment https://forums.phpfreaks.com/topic/288451-not-able-to-access-my-id-property/#findComment-1479305 Share on other sites More sharing options...
Solution eldan88 Posted May 15, 2014 Author Solution Share Posted May 15, 2014 @Jacques Why on earth would you tell me to rewrite the code when you haven't seen the whole thing and don;t know then intent of it. The reson why its the method is called "Get" is because it Getting the id's even though at the same time its assiging it to the attributes, and that's the way I like to put it in terms of Get. There are a lot of items that it needs to loop through and its not a single match, so before you comment on something first get a better understanding. @trq It probaly didn't make sense to you becuase I copied and pasted a small piece of the code. Next time I will make it more clear. But I found the soultion to my question Quote Link to comment https://forums.phpfreaks.com/topic/288451-not-able-to-access-my-id-property/#findComment-1479513 Share on other sites More sharing options...
Jacques1 Posted May 15, 2014 Share Posted May 15, 2014 Well, if you think your code is just great and doesn't need to be changed, there's nothing we can do about that. All we can do is give advice to people who want to learn and improve. It doesn't matter how the rest of your code looks like. The parts you've posted are objectively bad and make no sense in any context. Whether you fix this or leave it at that is up to you. Quote Link to comment https://forums.phpfreaks.com/topic/288451-not-able-to-access-my-id-property/#findComment-1479514 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.