Asked  7 Months ago    Answers:  5   Viewed   28 times

How can I deal with traits with methods of same name?

trait FooTrait {
  public function fooMethod() {
        return 'foo method';
  }

  public function getRow() {
        return 'foo row';
  }
}

trait TooTrait {
    public function tooMethod() {
        return 'too method';
    }

    public function getRow() {
        return 'too row';
    }
}

class Boo
{
    use FooTrait;
    use TooTrait;

    public function booMethod() {
        return $this->fooMethod();
    }
}

error,

Fatal error: Trait method getRow has not been applied, because there are collisions with other trait methods on Boo in...

What should I do about it?

And also, with two same method names, how can I get the method from trait FooTrait?

$a = new Boo;
var_dump($a->getRow()); // Fatal error: Call to undefined method Boo::getRow() in... 

Edit:

class Boo
{
    use FooTrait, TooTrait {
        FooTrait::getRow insteadof TooTrait;
    }

    public function booMethod() {
        return $this->fooMethod();
    }
}

what if I want to get the method getRow from TooTrait via Boo as well? Is it possible?

 Answers

85

PHP Documentation about conflicts:

If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.

To resolve naming conflicts between Traits used in the same class, the insteadof operator needs to be used to chose exactly one of the conflicting methods.

Since this only allows one to exclude methods, the as operator can be used to allow the inclusion of one of the conflicting methods under another name.

Example #5 Conflict Resolution

In this example, Talker uses the traits A and B. Since A and B have conflicting methods, it defines to use the variant of smallTalk from trait B, and the variant of bigTalk from trait A.

The Aliased_Talker makes use of the as operator to be able to use B's bigTalk implementation under an additional alias talk.

<?php
trait A {
    public function smallTalk() {
        echo 'a';
    }

    public function bigTalk() {
        echo 'A';
    }
}

trait B {
    public function smallTalk() {
        echo 'b';
    }

    public function bigTalk() {
        echo 'B';
    }
}

class Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
    }
}

class Aliased_Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
        B::bigTalk as talk;
    }
}

So in your case it could be

class Boo {
    use FooTrait, TooTrait {
        FooTrait::getRow insteadof TooTrait;
    }

    public function booMethod() {
        return $this->fooMethod();
    }
}

(it works even if you do separate use, but i think it's more clear)

Or use the as to declare an alias.

Wednesday, March 31, 2021
 
brombeer
answered 7 Months ago
53

This behavior is documented. From php.net (http://php.net/manual/en/language.oop5.traits.php):

An inherited member from a base class is overridden by a member inserted by a Trait. The precedence order is that members from the current class override Trait methods, which in return override inherited methods.

No reason for notices here.

Edit:

I took a look on some more serious literature to shed some light on this topic :) . Looks like that such behavior is a part of traits' definition. They are ment to work this way. This is from research "Traits: Composable Units of Behavior"(Proceedings of the European Conference on Object-Oriented Programming):

Another property of trait composition is that the composition order is irrelevant, and hence con?icting trait methods must be explicitly disambiguated (cf. section 3.5). Con?icts between methods de?ned in classes and methods de?ned by incorporated traits are resolved using the following two precedence rules.

– Class methods take precedence over trait methods.

– Trait methods take precedence over superclass methods. This follows from the ?attening property, which states that trait methods behave as if they were de?ned in the class itself.

You can read more here: http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf

Saturday, May 29, 2021
 
themihai
answered 5 Months ago
13

Use late static binding with static:

trait SomeAbility {
    public static function theClass(){
        return static::class;
    }
}

class SomeThing {
    use SomeAbility;
}

class SomeOtherThing {
    use SomeAbility;
}

var_dump(
    SomeThing::theClass(),
    SomeOtherThing::theClass()
);

// string(9) "SomeThing"
// string(14) "SomeOtherThing"

https://3v4l.org/mfKYM

Saturday, May 29, 2021
 
muaddhib
answered 5 Months ago
81

You need to make sure that the newly installed php command is executed, not the default one.

Add the folder where the correct php binary resides as the first item to the $PATH environment variable.

Saturday, May 29, 2021
 
oroshnivskyy
answered 5 Months ago
21

When the validation on the FormRequest fails, it checks to see if the request was ajax or if it accepts a json response. If so, it will return a json response with the 422 status code. If not, it will return a redirect to a specified url (previous, by default). So, in order to get the response on failure you're looking for (422), you need to make a json request or an ajax request.

JSON

To make a json request, you should use the json() method:

//post exam
$this->json('POST', 'modul/foo/exam', [
        'date' => '2016-01-01'
    ])
    ->assertResponseStatus(200);

//post exam again
$this->json('POST', 'modul/foo/exam', [
        'date' => 'some invalid date'
    ])
    ->assertResponseStatus(422);

There are also getJson(), postJson(), putJson(), patchJson(), and deleteJson() shortcut methods if you think that looks cleaner than passing the method as a parameter.

//post exam
$this->postJson('modul/foo/exam', [
        'date' => '2016-01-01'
    ])
    ->assertResponseStatus(200);

AJAX

To make an ajax request, you need to add in the ajax headers. For this, you can continue to use the post() method:

//post exam
$this->post('modul/foo/exam', [
        'date' => '2016-01-01'
    ], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'])
    ->assertResponseStatus(200);

//post exam again
$this->post('modul/foo/exam', [
        'date' => 'some invalid date'
    ], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'])
    ->assertResponseStatus(422);
Wednesday, August 11, 2021
 
Elxx
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share