PHP Traits (5.4)

Any tutorials (or useful resources) should go in here.
Post Reply
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

PHP Traits (5.4)

Post by bowersbros »

Okay, this is pretty much based on how I think that they work. I could be partly, or entirely wrong. And my code isn't being tested for this, since I haven't upgraded to 5.4 on my localhost yet.

But I digress.

So, since PHP is a single inheritance language. There are occasions, when you need things across multiple classes, but you can't because your already extending a class, and inheriting their properties.
class somename extends something,else {

}
is invalid. since you can't extend multiple classes.

SO what do you do?

Well, with php 5.4, a new languages construct called Traits is here

It saves you from having to copy and paste that other class over, and so on.
trait else {
 function someName(){
return 'hello';
}
}

class helloWorld {
 use else;
echo someName();
}

$hello = new helloWorld;
helloWorld::someName();

That should print out 'hello'

Thats my basic understanding of it anyway.

you can extend another class still, and if you were to have a naming conflict between two traits, then you would use the new insteadof operator

As such
trait one {
 function traitName(){
return 'one';
}
}
trait two {
function traitName(){
return 'two';
}
}

class name {
 use one,two;
 one:: traitName instead of two;

}
That would use the traitName from one, and not two by default. But two is still accessible by using two::traitName() inside of the class.

Alternatively, if you need access to either at any time, without referencing their class, then you can do that by using the as and renaming one of them temporarily.
/* same as above */

class name {

use one,two;
one:: traitName as traitNameOne;
}
That would allow for traitNameOne and traitName to be used, where they reference trait one and two respectively.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Traits (5.4)

Post by jacek »

Ohhhh a class can use multiple traits ? That makes them make a lot more sense !

I really doubt I will ever use this to be honest. I have gotten so used to designing everything with single inheritance in mind.
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: PHP Traits (5.4)

Post by bowersbros »

So yeah, 2 months down the line. Still haven't used it.

I am beginning to wonder why they bothered implementing this, when its fairly clear that it isn't all that useful, since people have worked around PHP being a single inheritance language for such a long time.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: PHP Traits (5.4)

Post by jacek »

Yeah I can’t really see a sensible usage example that can;t easily be done in other ways. I guess it's a nice feature though.
Image
Post Reply