Difference between revisions of "PHP/trait/use"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP‎ | trait
Jump to navigation Jump to search
(Created page with "==About== ''This sense of the '''use''' keyword is for including traits; there's another sense that only relates to namespaces.'' The '''use''' keyword allows a trait...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==About==
 
==About==
''This sense of the '''use''' keyword is for including traits; there's another sense that only relates to namespaces.''
+
''This sense of the '''use''' keyword is for including traits; for other senses, see [[PHP/use]].''
  
The '''use''' keyword allows a [[../|trait]] to be used within a class or another trait.
+
The '''use''' keyword allows a [[../|trait]] to be used within a class or another trait. There unfortunately does not seem to be a page specifically documenting its syntax; most (but not all) of the applicable syntax can be found on the [https://www.php.net/manual/en/language.oop5.traits.php traits] documentation page.
 
===Basic Form===
 
===Basic Form===
 
<syntaxhighlight lang=php>
 
<syntaxhighlight lang=php>
Line 40: Line 40:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
===Form for Pseudoparent Calls===
 
===Form for Pseudoparent Calls===
I know this exists, but I'm still looking for the docs.
+
This is currently not documented on the traits page; I'm not sure where I found it.
 +
 
 +
Starting again with fresh example code, here is the problem:
 +
<syntaxhighlight lang=php>
 +
trait tSample1 {
 +
    function Func1() {}
 +
}
 +
trait tSample2 {
 +
    use tSample1;
 +
    function Func1() {
 +
        parent::Func1();  // This doesn't work! A "use"d trait is not a parent.
 +
    }
 +
}
 +
</syntaxhighlight>
 +
To fix this, alter tSample2 thusly:
 +
<syntaxhighlight lang=php>
 +
trait tSample2 {
 +
    use tSample1 { tSample1::Func1 as parent_Func1; }
 +
    function Func1() {
 +
        parent_Func1();  // We'll pretend we're a real family. (Are you my mummy?)
 +
    }
 +
}
 +
</syntaxhighlight>
 +
In case you need to alias multiple functions (starting over):
 +
<syntaxhighlight lang=php>
 +
// 2022-04-13 I reconstructed this syntax through trial-and-error, but I swear it's in the docs. Somewhere.
 +
trait tSample1 {
 +
    function Func1() {}
 +
    function Func2() {}
 +
    function Func3() {}
 +
}
 +
trait tSample2 {
 +
    use tSample1 {
 +
      tSample1::Func1 as parent_Func1;
 +
      tSample1::Func2 as parent_Func2;
 +
      tSample1::Func3 as parent_Func3;
 +
    }
 +
    function Func1() { $this->parent_Func1(); }
 +
    function Func2() { $this->parent_Func2(); }
 +
    function Func3() { $this->parent_Func3(); }
 +
}
 +
</syntaxhighlight>
 
===Other===
 
===Other===
 
You can also change the visibility (public/protected/private) of a function with "use", but I've never had occasion to... use... this feature.
 
You can also change the visibility (public/protected/private) of a function with "use", but I've never had occasion to... use... this feature.
 +
==Errors==
 +
===Required Trait wasn't added===
 +
<blockquote>
 +
'''Fatal error''': Required Trait {{arg|trait name}} wasn't added to {{arg|class or trait name}} in '''{{arg|filename}}''' on line '''{{arg|line #}}'''
 +
</blockquote>
 +
This may result from aliasing methods from multiple traits but not including all of them in the "use" statement.
 +
* '''Example''': <syntaxhighlight lang=php inline>use A { A::F1 as parent_F1; B::F2 as parent_F2; }</syntaxhighlight> should be <syntaxhighlight lang=php inline>use A,B {...</syntaxhighlight>

Latest revision as of 14:43, 27 May 2024

About

This sense of the use keyword is for including traits; for other senses, see PHP/use.

The use keyword allows a trait to be used within a class or another trait. There unfortunately does not seem to be a page specifically documenting its syntax; most (but not all) of the applicable syntax can be found on the traits documentation page.

Basic Form

trait tSample1 {}
class cSample1 { use tSample1; }
trait tSample2 { use tSample1; }

Form for Multiple Traits

Building on the above example:

class cSample2 { use tSample1, tSample2; }
// This ^ is equivalent to:
class cSample2 {
    use tSample1;
    use tSample2;
}

Form for Conflict Resolution

Starting afresh...

trait tSample1 {
    function Func1() {}
    function Func2() {}
}
trait tSample2 {
    function Func1() {}
    function Func2() {}
}
// If we want to use both tSample3 and tSample4, how do we indicate which version of Func1() to use?
// Here's the syntax for that:
class cSample1 {
    use tSample1, tSample2 {
        tSample2::Func1 insteadof tSample1;
        tSample1::Func2 insteadof tSample2;
    }
}

Form for Pseudoparent Calls

This is currently not documented on the traits page; I'm not sure where I found it.

Starting again with fresh example code, here is the problem:

trait tSample1 {
    function Func1() {}
}
trait tSample2 {
    use tSample1;
    function Func1() {
        parent::Func1();  // This doesn't work! A "use"d trait is not a parent.
    }
}

To fix this, alter tSample2 thusly:

trait tSample2 {
    use tSample1 { tSample1::Func1 as parent_Func1; }
    function Func1() {
        parent_Func1();  // We'll pretend we're a real family. (Are you my mummy?)
    }
}

In case you need to alias multiple functions (starting over):

// 2022-04-13 I reconstructed this syntax through trial-and-error, but I swear it's in the docs. Somewhere.
trait tSample1 {
    function Func1() {}
    function Func2() {}
    function Func3() {}
}
trait tSample2 {
    use tSample1 {
      tSample1::Func1 as parent_Func1;
      tSample1::Func2 as parent_Func2;
      tSample1::Func3 as parent_Func3;
    }
    function Func1() { $this->parent_Func1(); }
    function Func2() { $this->parent_Func2(); }
    function Func3() { $this->parent_Func3(); }
}

Other

You can also change the visibility (public/protected/private) of a function with "use", but I've never had occasion to... use... this feature.

Errors

Required Trait wasn't added

Fatal error: Required Trait <trait name> wasn't added to <class or trait name> in <filename> on line <line #>

This may result from aliasing methods from multiple traits but not including all of them in the "use" statement.

  • Example: use A { A::F1 as parent_F1; B::F2 as parent_F2; } should be use A,B {...