<commit_before># Class, Interface, and Trait Inheritance

Whether by constructor parameters or setter methods, each class instantiated through the _Container_ "inherits" the values of its parents by default. This means we can set a constructor parameter or setter method value on a parent class, and the child class will use it (that is, unless we set an overriding value on the child class).

## Constructor Parameter Inheritance

Let's say we have this parent class and this child class:

```php
class ExampleParent
{
    protected $foo;
    protected $bar;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}

class ExampleChild extends ExampleParent
{
    protected $baz;

    public function setBaz($baz)
    {
        $this->baz = $baz;
    }
}
```

We can specify the default values for every class that extends _ExampleParent_ through the `$di->params` and `$di->setter` values for the _ExampleParent_.

```php
$di->params['ExampleParent']['foo'] = 'parent_foo';
$di->setter['ExampleParent']['setBar'] = 'parent_bar';
```

When we call `$di->newInstance('ExampleChild')`, the child class will have inherited the defaults from the parent.

We can always override the inherited values by specifying them for the child class directly:

```php
$di->params['ExampleChild']['foo'] = 'child_foo';
$di->setter['ExampleChild']['setBaz'] = 'child_baz';
```

Note that classes extended from the child class will then inherit those new values. In this way, constructor parameter and setter method values are propagated down the inheritance hierarchy.



## Setter Method Inheritance

If a class uses a setter method, whether by extending a parent class, using a trait, or implementing an interface, we can specify the default value for that setter method in relation to the parent class, trait, or interface. That value will then be applied by default in every class that extends that parent class, uses that trait, or implements that interface.

For example, let's say we have this trait, interface, and class:

```php
trait ExampleFooTrait
{
    public function setFoo($foo)
    {
        $this->foo = $foo;
    }
}

interface ExampleBarInterface
{
    public function setBar($bar);
}

class ExampleWithTraitAndInterface implements ExampleBarInterface
{
    use ExampleFooTrait;

    protected $foo;
    protected $bar;

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}
```

We then define the default setter method values on the trait and interface:

```php
$di->setter['ExampleFooTrait']['setFoo'] = 'foo_value';
$di->setter['ExampleBarInterface']['setBar'] = 'bar_value';
```

When we call `$di->newInstance('ExampleWithTraitAndInterface')`, those setter methods will be called by the _Container_ with those values.

Note that if we have class-specific `$di->setter` values, those will take precedence over the trait and interface setter values.
<commit_msg>reorganize doc page on inheritance<commit_after># Class, Interface, and Trait Inheritance

## Class Inheritance

Each class instantiated through the _Container_ "inherits" the constrctor parameter and setter method values of its parents by default. This means we can specify a constructor parameter or setter method value on a parent class, and the child class will use it (that is, unless we set an overriding value on the child class).

Let's say we have this parent class and this child class:

```php
class ExampleParent
{
    protected $foo;
    protected $bar;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}

class ExampleChild extends ExampleParent
{
    protected $baz;

    public function setBaz($baz)
    {
        $this->baz = $baz;
    }
}
```

If we define the constructor parameters and setter method values for the parent ...

```php
$di->params['ExampleParent']['foo'] = 'parent_foo';
$di->setter['ExampleParent']['setBar'] = 'parent_bar';
```

... then when we call `$di->newInstance('ExampleChild')`, the child class will "inherit" those values as defaults.

We can always override the "inherited" values by specifying them for the child class directly:

```php
$di->params['ExampleChild']['foo'] = 'child_foo';
$di->setter['ExampleChild']['setBaz'] = 'child_baz';
```

Classes extended from the child class will then inherit those new values. In this way, constructor parameter and setter method values are propagated down the inheritance hierarchy.

## Interface And Trait Inheritance

If a class exposes a setter method by implementing an interface or using a trait, we can specify the default value for that setter method on the interface or trait. That value will then be applied by default to every class that extends that implements that interface or uses that trait.

For example, let's say we have this interface, trait, and class:

```php
interface ExampleBarInterface
{
    public function setBar($bar);
}

trait ExampleFooTrait
{
    public function setFoo($foo)
    {
        $this->foo = $foo;
    }
}

class Example implements ExampleBarInterface
{
    use ExampleFooTrait;

    protected $foo;
    protected $bar;

    public function setBar($bar)
    {
        $this->bar = $bar;
    }
}
```

We can define the default setter method values on the trait and interface:

```php
$di->setter['ExampleFooTrait']['setFoo'] = 'foo_value';
$di->setter['ExampleBarInterface']['setBar'] = 'bar_value';
```

When we call `$di->newInstance('Example')`, those setter methods will be called by the _Container_ with those values.

Note that if we have class-specific `$di->setter` values, those will take precedence over the trait and interface setter values.

{'fraction_non_alphanumeric': 0.09615384615384616, 'fraction_numerical': 0.0, 'mean_word_length': 4.11790780141844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534068', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1556, 'n_words': 682}