PHP Override Method Overrides Parent Class Property

I use Laravel 5.1, but this does not apply to this structure, it is rather a general PHP question.

There is a parent class with the specified characteristics:

namespace Illuminate\Foundation\Auth; use Illuminate\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, Authorizable, CanResetPassword; } 

Then I have a User class about which I want to say that it extends from this:

 namespace App\Api\V1\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use Zizaco\Entrust\Traits\EntrustUserTrait; class User extends Authenticatable { use EntrustUserTrait { EntrustUserTrait::can insteadof \Illuminate\Foundation\Auth\Access\Authorizable; } } 

EntrustUserTrait has a can() method that conflicts with the Authorizable attribute. However, the Authorizable property is in the parent class, so this causes the Required Trait wasn't added to App\Api\V1\Models\User error.

I searched around and there was a lot of information about overriding traits declared in a child class, but I cannot find anything about redefining traits from a parent class.

+5
source share
1 answer

Consider the following code:

 <?php trait AA { function f() { echo "I'm AA::f".PHP_EOL; } } class A { use AA; } trait BB { function f() { echo "I'm BB::f".PHP_EOL; } } class B extends A { use BB; } $b = new B(); $b->f(); 

I BB :: f

I believe features work like copy-paste code. The tag code is treated as "copied" code in the class in which it is used, so you can pretend that the tag is not actually inherited, but it is part of the parent class.

+5
source

Source: https://habr.com/ru/post/1246481/


All Articles