14 Replies - 6142 Views - Last Post: 14 December 2011 - 09:34 AM

#1 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

PHP Traits

Post icon  Posted 05 August 2011 - 05:00 PM

Traits.

Discuss.
Is This A Good Question/Topic? 3
  • +

Replies To: PHP Traits

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9032
  • View blog
  • Posts: 33,508
  • Joined: 27-December 08

Re: PHP Traits

Posted 05 August 2011 - 05:21 PM

They look interesting. Very similar to interfaces, only with non-abstract methods. Neat!
Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1210
  • View blog
  • Posts: 4,124
  • Joined: 27-January 10

Re: PHP Traits

Posted 05 August 2011 - 05:51 PM

I don't use PHP, but doesn't that language have interfaces?
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 934
  • View blog
  • Posts: 2,329
  • Joined: 15-February 11

Re: PHP Traits

Posted 05 August 2011 - 06:37 PM

It's absolutely beautiful. The whole single inheritance thing is a major pain at times. The closest thing you can do is pass objects to your class or simply initialize the object within your class if you want to access it's methods. I'm just sorry a lot of hosting companies are still on 5.2.*. It'll be a while before we can fully embrace this feature.
Was This Post Helpful? 1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: PHP Traits

Posted 06 August 2011 - 12:42 AM

View PostSergio Tapia, on 06 August 2011 - 02:51 AM, said:

I don't use PHP, but doesn't that language have interfaces?

Traits ain’t Interfaces. Traits are somewhat like building blocks, while interfaces are for the API.
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: PHP Traits

Posted 06 August 2011 - 04:24 AM

*
POPULAR

To me, Traits are a stupid way of getting around the fact that we don't have multiple inheritance.

They've come up with a whole system just to allow reusable code, which you would do in most other languages via multiple inheritance of other classes. What we're going to have now is basically instead of something that makes sense such as:
class Developer extends Human, Geek, Logical

We will have random mixed versions of it including:
class Developer {
    use Human, Geek, Logical;

class Developer extends Human {
    use Geek, Logical;


Then we'll get random mutated versions of our written traits used by developers who think they want something that they shouldn't:
trait Geek {
    public function DoGeekyStuff() {
        // Do some geeky stuff! This should be public so others can call it
    }
}

class Developer {
    use Human, Geek { DoGeekyStuff as private }, Logical; // Haha, screw you public API rubbishness!
}


-------------------------------------------------------

I think what annoys me most about this whole thing is that they must've spent so much time on this when they haven't fixed inherent problems with the language that have been there from the beginning.

Like the fact we can't do this:
$result = new Class()->doSomething();


But we can do this..
function newClass() {
    return new Class();
}
$result = newClass()->doSomething();


It almost made me want to write a wrapper for creating new classes, such as:
function _($className) {
    if (!class_exists($className))
        return null;
    return new $className;
}

and just use that anywhere I wanted a new class, but the _() function already exists. Perhaps I should call it a()...

Also, we can't do this:
function blabla() {
    return array('test' => 'test!');
}
$test = blabla()['test'];


They would be soooooooooooo much more useful than anything Traits could ever provide to us. I really don't think the PHP dev team has a clue what people want.

Surely somebody shares my views on this..

[/rant]

This post has been edited by RudiVisser: 06 August 2011 - 04:33 AM

Was This Post Helpful? 5
  • +
  • -

#7 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: PHP Traits

Posted 06 August 2011 - 05:50 AM

Interesting, I had not heard of traits in PHP, or any other language for that matter. I could see lots of ways to implement these on the project I spend most of my time on at work. Now only if we had a web host that was running higher than PHP ver 5.2...
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: PHP Traits

Posted 06 August 2011 - 10:01 AM

Actually I think is the classic bloat that I have seen the PHP community do since about 5.3. Honestly, I think traits here are crap. While they are trying to compete with things like Ruby mixins, there is no real need for them. They are just going to make the language that more complex for little if any true gain.

I find it sad that PHP is going this direction. RudiVisser brought up a lot of my concerns when I saw this feature and you then have to understand a new overriding hierarchy to use it... then of course we have to go explaining it to n00bs of why it is like this when there is no good reason.

Another PHP fail as far as I am concerned. :(
Was This Post Helpful? 3
  • +
  • -

#9 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 745
  • View blog
  • Posts: 1,523
  • Joined: 30-January 09

Re: PHP Traits

Posted 10 August 2011 - 01:28 AM

View PostRudiVisser, on 06 August 2011 - 05:24 AM, said:

Surely somebody shares my views on this..

[/rant]

Yeah I do. After getting my head around what traits do, they seem to do very little at all. Surely trait behaviour can be achieved by something like this:
class myTraitClass()
{
  public function myTrait()
  {
    // Does something
  }
}

class whyUseTraits()
{
  protected $myTraits;

  public function __construct()
  {
    $this->myTraits = new myTraitClass();
  }

  public function noNeedForTraits()
  {
    return $this->myTraits->myFunction();
  }
}



I dunno, maybe I've got it wrong and I'm misinterpreting.

PS, good call on the $result = new Class()->doSomething(); I'm baffled as to the fact that you can't do that in PHP.
Was This Post Helpful? 0
  • +
  • -

#10 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: PHP Traits

Posted 10 August 2011 - 02:06 AM

View Poste_i_pi, on 10 August 2011 - 09:28 AM, said:

View PostRudiVisser, on 06 August 2011 - 05:24 AM, said:

Surely somebody shares my views on this..

[/rant]

Yeah I do. After getting my head around what traits do, they seem to do very little at all. Surely trait behaviour can be achieved by something like this:
..snip..



I dunno, maybe I've got it wrong and I'm misinterpreting.

Yes that of course would be possible, but traits are meaning to have functions actually "be a part" of the class they're used within.

So essentially traits are a fake version of multiple inheritance. If you had one class like yours, then it would be simple enough to do class whyUseTraits extends myTraitClass, but traits will "help" when you need to do something like class whyUseTraits extends myTraitClass, anotherTraitClass, yetAnotherTraitClass... It would be so much better if they just actually did that.

But like I said, PHP's development team is clearly thick, it's been going in the wrong direction for quite some time in my eyes.

And yes the whole instantiation/array thing is an absolute pain in the ass. Compared to the usefulness/time investment to get these implemented and I'm fairly certain that they would be much more well received by the community than traits are.

Don't they have some sort of focus group thing thinking if this would be a good idea?! I mean a focus group that contains non-PHP developers, of course.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: PHP Traits

Posted 10 August 2011 - 02:18 AM

View PostRudiVisser, on 06 August 2011 - 01:24 PM, said:

Also, we can't do this:
function blabla() {
    return array('test' => 'test!');
}
$test = blabla()['test'];


They would be soooooooooooo much more useful than anything Traits could ever provide to us. I really don't think the PHP dev team has a clue what people want.

the Array Dereferencing feature of PHP 5.4 …

View PostRudiVisser, on 06 August 2011 - 01:24 PM, said:

Like the fact we can't do this:
$result = new Class()->doSomething();


But we can do this..
function newClass() {
    return new Class();
}
$result = newClass()->doSomething();

at least it has been in discussion (ref.)
Was This Post Helpful? 0
  • +
  • -

#12 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: PHP Traits

Posted 10 August 2011 - 02:30 AM

View PostDormilich, on 10 August 2011 - 10:18 AM, said:

the Array Dereferencing feature of PHP 5.4 …

I had completely missed array dereferencing!

View PostDormilich, on 10 August 2011 - 10:18 AM, said:

at least it has been in discussion (ref.)

I saw something on the old mailing list that dated this back to around 2007, or perhaps even before PHP5's time.. Will be a miracle if they do it.

The patch files are so simple and it wouldn't really be too difficult to test for regressions, you'd hope.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2890
  • View blog
  • Posts: 7,535
  • Joined: 08-June 10

Re: PHP Traits

Posted 10 August 2011 - 02:55 AM

View PostRudiVisser, on 10 August 2011 - 11:30 AM, said:

I saw something on the old mailing list that dated this back to around 2007, or perhaps even before PHP5's time.. Will be a miracle if they do it.

I can’t spot it in the alpha1 changelog.

I also find the Session class proposal interesting.
Was This Post Helpful? 0
  • +
  • -

#14 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 745
  • View blog
  • Posts: 1,523
  • Joined: 30-January 09

Re: PHP Traits

Posted 25 October 2011 - 05:27 PM

I take back my original comment. I've just found a use for traits where I can't feasibly use inheritance without violating the KISS, DRY, or YAGNI principles. It's a pity they're PHP 5.4 and I'm developing on 5.3. Guess I'll have to make a trait-like abstract class high up on the inheritance tree, and replace it once I upgrade my server :pinch:
Was This Post Helpful? 0
  • +
  • -

#15 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: PHP Traits

Posted 14 December 2011 - 09:34 AM

View PostDormilich, on 10 August 2011 - 09:55 AM, said:

View PostRudiVisser, on 10 August 2011 - 11:30 AM, said:

I saw something on the old mailing list that dated this back to around 2007, or perhaps even before PHP5's time.. Will be a miracle if they do it.

I can’t spot it in the alpha1 changelog.


DUUUUUUUUUUUUUUDE - 5.4 RC1:

Quote

Added class member access on instantiation (e.g. (new Foo)->bar()).


Yayyyyyy :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1