Jump to content

Not able to access my id property


eldan88
Go to solution Solved by eldan88,

Recommended Posts

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

}




}
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution

@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

Link to comment
Share on other sites

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.

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.