tighten / parental

Use single table inheritance in your Laravel app
MIT License
1.36k stars 98 forks source link

Registering an observer on the parent class with a self referencing child throws `Maximum function nesting` #59

Closed BertvanHoekelen closed 4 years ago

BertvanHoekelen commented 5 years ago

When you register an observer on the parent class that has self as childType it throws Error : Maximum function nesting level of '256' reached, aborting!.

Example

<?php

class User
{
    use \Tightenco\Parental\HasChildren;

    protected $childTypes = [
        'super_admin' => Admin::class,
        'admin' => self::class
    ];
}
// AppServiceProvider.php

class AppServiceProvider
{
    public function boot(Environment $environment)
    {
          User::Observe(UserObserver::class);
    }
}

Solution

This line should check if $childClass is not an instance of self.

foreach ((new self)->childTypes as $childClass) {
    if ($childClass !== self::class) {
        $childClass::registerModelEvent($event, $callback);
    }
}