# Correct using component in another components

**URL:** <https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292>\
**Category:** Need Help\
**Created:** [February 17, 2020, 8:59am UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292 "2020-02-17T08:59:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilyav](https://avatars.discourse-cdn.com/v4/letter/i/c4cdca/32.png) [@ilyav](https://discourse.cakephp.org/u/ilyav)\
**Post date:** [February 17, 2020, 8:59am UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/1 "2020-02-17T08:59:30Z")

</div>

Hi. I need to call component in another component. In official documentation CakePHP they recommend add component to variable $components. But I need call component in different functions and send different params. How can I call component?

In controller I use loadComponent and put params to component initialize method (constructor)

Thanks

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 17, 2020, 2:04pm UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/2 "2020-02-17T14:04:51Z")

</div>

Please share more details about your scenario. “call component in different function and send different params” is extremely generic, so we can’t really give a meaningful reply.

---

<div class="post-metadata">

**Author:** ![ilyav](https://avatars.discourse-cdn.com/v4/letter/i/c4cdca/32.png) [@ilyav](https://discourse.cakephp.org/u/ilyav)\
**Post date:** [February 17, 2020, 2:10pm UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/3 "2020-02-17T14:10:54Z")

</div>

Okay. I have component witch create socket connection via proxy and I have another component with functions which connect to different endpoints. I need load component in every function with different params.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 17, 2020, 9:50pm UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/4 "2020-02-17T21:50:07Z")

</div>

Sounds like those parameters should be included in whatever call you’re making to the component in “every function”, rather than included in the configuration when you add the component.

---

<div class="post-metadata">

**Author:** ![ilyav](https://avatars.discourse-cdn.com/v4/letter/i/c4cdca/32.png) [@ilyav](https://discourse.cakephp.org/u/ilyav)\
**Post date:** [February 18, 2020, 7:12am UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/5 "2020-02-18T07:12:28Z")

</div>

Yes, but this parameters must be passed to the component constructor. When I add component via $components variable I can put my custom parameters, but how can I call component with different parameters in another place in my component?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [February 18, 2020, 2:13pm UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/6 "2020-02-18T14:13:52Z")

</div>

Why must they be passed in the constructor? Maybe share some code to demonstrate your problem.

---

<div class="post-metadata">

**Author:** ![ilyav](https://avatars.discourse-cdn.com/v4/letter/i/c4cdca/32.png) [@ilyav](https://discourse.cakephp.org/u/ilyav)\
**Post date:** [February 18, 2020, 2:17pm UTC](https://discourse.cakephp.org/t/correct-using-component-in-another-components/7292/7 "2020-02-18T14:17:43Z")

</div>

I have resolve my problem.

Component A:

```
class A extends Component 
{
    //constructor
    public function initialize(array $config)
    {
        parent::initialize($config);
        //logic for handling params from $config
    }
    //another methods
}

```

Component B:

```
class B extends Component 
{
    $components = ['A'];

    public function funcB()
    {
        $this->A->initialize([
            'param' => value
        ]);
        //call another method of A component
    }
}
```
