1

I would like to pass one variable into an array. I have two classes. One MenuClass is used to register menus - and I need the variable populated as an array. I will be using the second class extended, and passing in multiple menus. So I need each run to insert another item into the Main->loaded_menus[] array.

They are loaded in different files, but loaded at the same time. Here is a very basic example of what I am working with...

class Main {

    public $loaded_Menus = array();

    public function __construct() {}

}

class MenuClass {

    public $name;
    public $id;

    public function build($id, $name) {

        $this->id = $id;
        $this-name = $name;

        $populate = new Main;
        $populate->loaded_Menus[$this->id] = $this->id;
    }
}
4
  • In your example which one is extended? Commented Mar 18, 2014 at 2:22
  • MenuClass would be the one that is extendable. The Main class is used as the primary plugin class. Commented Mar 18, 2014 at 2:24
  • @Javad please don't edit the OP to answer the question. Point out the "fix" in your answer instead. Commented Mar 18, 2014 at 2:36
  • My answer is below, and I mean the below answer not the edited question Commented Mar 18, 2014 at 2:38

1 Answer 1

1

When you extend from another class all public properties will be inherited, therefore you don't need to create a new instance of the parent class

class MenuClass extends Main {

   public $name;
   public $id;

   public function build($id, $name) {

     $this->id = $id;
     $this-name = $name;

     $this->loaded_Menus[$this->id] = $this->id;
   }
}

If you want to keep all previous values in your parent class then define it as static; so while your page is alive the values will be kept

class Main {

   public static $loaded_Menus = array();
   public function __construct() {}

}

class MenuClass extends Main {

  public $name;
  public $id;

  public function build($id, $name) {

     $this->id = $id;
     $this-name = $name;

     parent::$loaded_Menus[$this->id] = $this->id;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.