How to call a function from a trait in this situation?

Suppose there is such a situation:

trait TraitTest
{
 public function myFunction()
{
 return 'myFunction()';
}

 public function getDescription()
{
 return 'getDescription()';
}
}


class MyClass
{
 use TraitTest;

 public function __construct()
{

}
}

$MyClass = new MyClass;
echo $MyClass->myFunction();


The result of this code will look like this:
myFunction()

Now there is a need to redefine the function myFunction:
class MyClass
{
 use TraitTest;

 public function __construct()
{

}

 public function myFunction()
{
 return 'myFunction2()';
}
}


Everything works without issues. But what if you need inside an overridden function myFunction() call myFunction from the function trait.

Something like this:
public function myFunction()
{
 //return self::myFunction();
 //return $this->myFunction();
 return parent::myFunction();
 }


In General, a hike on PHP.no such mentions all sorts of pointers to a class give error.
July 9th 19 at 14:01
2 answers
July 9th 19 at 14:03
Use the manual, Luke.

// EDIT for those who nootkas do not want to read docks.

<?php

trait TraitTest
{
 public function myFunction()
{
 return 'trait myFunc';
}

 public function getDescription()
{
 return 'trait getDesc';
}
}


class MyClass
{
 use TraitTest {
 TraitTest::myFunction as protected traitMyFunc;
}

 public function myFunction()
{
 echo 'MyClass myFunc';
 echo $this--->traitMyFunc();
}
}

$MyClass = new MyClass();
echo $MyClass->myFunction();
once again the question, take a look please. - Justice commented on July 9th 19 at 14:06
: RFTM. - vito.Hoeg commented on July 9th 19 at 14:09
: RTFM =(. The guide Pro is not mentioned. You can specify an alias and call, but then the 2 functions will be available in the external API, this is unacceptable. - Justice commented on July 9th 19 at 14:12
you do not mind the presence of redundant functions in the external API. When I wrote the question was not even considered the option of an alias, as for me, all this is unacceptable. - Justice commented on July 9th 19 at 14:15
: updated the answer. see how Elias is. - vito.Hoeg commented on July 9th 19 at 14:18
: Write the same as without the alias do. - Adeline.Goldner commented on July 9th 19 at 14:21
$this will be passed correctly? - vito.Hoeg commented on July 9th 19 at 14:24
: Check. Spoiler - will. - Adeline.Goldner commented on July 9th 19 at 14:27
July 9th 19 at 14:05
Connect like this:
use MyTrait {
 MyTrait::myFunction as myTraitFunction;
}

then call:
public function myFunction()
{
$this->myTraitFunction();
}
option, but the problem is that from an external API, then the option will be available myTraitFunction, and this is very not good in my case. - Justice commented on July 9th 19 at 14:08
: then tell her to crate protected - vito.Hoeg commented on July 9th 19 at 14:11
if a trait in a vendor ? - Justice commented on July 9th 19 at 14:14

Find more questions by tags PHP