How to override trait function and call it from the overridden function?

Asked
Active3 hr before
Viewed126 times

8 Answers

function
90%

Now (new MyClass)->sayHi(); will output "class says hi".,The trait function can be overridden simply by defining a function with the same name in the class.,Now (new MyClass)->sayHi(); will output "trait says hi and class says hi".,Executing (new MyClass)->sayHi(); will output "trait says hi".

Let's create a simple trait and use it in a class.

trait MyTrait {
   public
   function sayHi() {
      echo 'trait says hi';
   }
}

class MyClass {
   use MyTrait;
}
load more v
88%

7 As Phillip mentioned (I think), how would you do this for one method of a trait while still including all other methods of the same trait as normal? Preferrably without explicitly referencing each method. – Gannet May 1 '17 at 3:41 , 27 just to clarify - once your class defines the same method, it automatically overrides the trait's. The trait fills in the method as @ircmaxell mentions when it's empty. – Yehosef Jun 8 '14 at 12:43 ,This just saves classes from having to mess with the 'use' statement if they want to override the function, since they can still call the protected function internally., I do agree that it's interesting. It works, because for the extending class, the trait methods actually exist in the parent class. Personally I think it can be convenient if you find yourself in that situation, but I would not recommend doing it by design. – AeonOfTime Jan 29 at 9:08

Your last one was almost there:

trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A {
      calc as protected traitcalc;
   }

   function calc($v) {
      $v++;
      return $this - > traitcalc($v);
   }
}
load more v
72%

How to override trait function and call it from... ,66223/override-trait-function-call-from-the-overridden-function, Web Development (1,457) ,You can also provide for ways to override, but still access the trait method as follows:

This code doesn't work, and I cannot find a way to call a trait function like it was inherited. I tried calling self::calc($v), static::calc($v), parent::calc($v), A::calc($v) and the following:

trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A {
      calc as traitcalc;
   }

   function calc($v) {
      $v++;
      return traitcalc($v);
   }
}
load more v
65%

This just saves classes from having to mess with the 'use' statement if they want to override the function, since they can still call the protected function internally.,You can also provide for ways to override, but still access the trait method as follows:,This code doesn't work, and I cannot find a way to call a trait function like it was inherited. I tried calling self::calc($v), static::calc($v), parent::calc($v), A::calc($v) and the following:,Another variation: Define two functions in the trait, a protected one that performs the actual task, and a public one which in turn calls the protected one.

Scenario:

trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A;

   function calc($v) {
      $v++;
      return A::calc($v);
   }
}

print(new MyClass()) - > calc(2); // should print 4

This code doesn't work, and I cannot find a way to call a trait function like it was inherited. I tried calling self::calc($v), static::calc($v), parent::calc($v), A::calc($v) and the following:

trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A {
      calc as traitcalc;
   }

   function calc($v) {
      $v++;
      return traitcalc($v);
   }
}
load more v
75%

How to can check the string exists in sentence or not , Laravel configuration error: The cipher and / or key length are invalid , How to find if a variable contains certain character or not using php ? , Find first occurence numerical position in a string by php

trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A;

   function calc($v) {
      $v++;
      return A::calc($v);
   }
}

print(new MyClass()) - > calc(2); // should print 4
trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A {
      calc as traitcalc;
   }

   function calc($v) {
      $v++;
      return traitcalc($v);
   }
}
load more v
40%

This code doesn't work, and I cannot find a way to call a trait function like it was inherited. I tried calling self::calc($v), static::calc($v), parent::calc($v), A::calc($v) and the following:,The trait is not a class. You can't access its members directly. It's basically just automated copy and paste...,Is there a way to make it work or must I override completely the trait function which is much more complex than this :),PHP Class Using Same Name as Trait Function Refer to the question I just asked above here. Here was my original code. trait sampletrait{ function hello(){ echo...

Scenario:

trait A {
   function calc($v) {
      return $v + 1;
   }
}

class MyClass {
   use A;

   function calc($v) {
      $v++;
      return A::calc($v);
   }
}

print(new MyClass()) - > calc(2); // should print 4
load more v
22%

If two Traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved. , 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 turn override inherited methods. , Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas. , If a trait defines a property then a class can not define a property with the same name unless it is compatible (same visibility and initial value), otherwise a fatal error is issued.

Hello World!
load more v
60%

Let’s start by defining a very simple trait as an example called FooTrait that contains the definition for a public method called bar():-,The methods of the attached trait essentially work as though they’ve been copied and pasted into our Example class.,If we were to then define a new bar() method in our Example class that will override the trait’s bar() method then this new method would be run instead:-,We can then assign that trait to a class and call bar() as a method of the class:-

Let’s start by defining a very simple trait as an example called FooTrait that contains the definition for a public method called bar():-

trait FooTrait {
   public
   function bar() {
      return 'A';
   }
}
load more v

Other "function-undefined" queries related to "How to override trait function and call it from the overridden function?"