PHP Traits

Please use this forum to ask for a new feature or to change an existing feature.
Merci d'utiliser ce forum pour demander de nouvelles fonctionnalités ou la modification de fonctionnalités existantes.

PHP Traits

Postby fbernoldi » Tue 5 Dec 2017 13:34

Hi,

Can we stereotype relations as "trait" for PHP for the artifact to generate the necessary "use" for the feature?

http://php.net/manual/en/language.oop5.traits.php

Normally I edit the artifact manually and stereotype a generalization relation to model it (set it to not generated in PHP).

Thanks!

Federico.
fbernoldi
 
Posts: 40
Joined: Mon 10 Jul 2017 14:15

Re: PHP Traits

Postby Bruno Pagès » Wed 6 Dec 2017 08:28

Hi,

I edit the artifact manually

why, what do you add in the artifact definition ?

stereotype a generalization relation to model it (set it to not generated in PHP)

I do not understand, how a generalization can produce a use into a class definition ?

A trait definition can be modeled through a class just replacing "class" by "trait" in its php definition.
The use form into the other classes can be modeled through a directional relation modifying its php definition to be "use ${type};" for instance.
The generation is done from free forms with keywords to allow that kind of changes, not just to complicate the generations/reverse/roundtrip ;)
ImageAuthor of Bouml
Bruno Pagès
 
Posts: 603
Joined: Mon 20 Feb 2012 09:23
Location: France

Re: PHP Traits

Postby fbernoldi » Wed 6 Dec 2017 10:34

Hello,

why, what do you add in the artifact definition ?


class SomeClass {
@use
....
}

and on the class I define a property with that name and type "use SomeTrait;"
I do not understand, how a generalization can produce a use into a class definition ?

I model it as a generalization since using traits in PHP it's kinda like having multiple inheritance, maybe i'm wrong with this.
A trait definition can be modeled through a class just replacing "class" by "trait" in its php definition.

This I do it right :D
The use form into the other classes can be modeled through a directional relation modifying its php definition to be "use ${type};" for instance.

This is way better than the @use I'm doing now XD.

The thing is, unless you add type a stereotype on those relations, or a stereotype on the class you are changing to trait on the php definition, when you are looking it on a bouml's class diagram it's shown like a regular relation and a regular class and you can't tell that it's a trait.

My thought was, if you stereotype a class as an interface, it's shown on the diagrams as an interface, produces a interface on the code and when you add a realisation to that interface it also produces the "implements" that interface; so, why not stereotype a class to trait and that bouml do "the same" there.

It's not that i'm lazy or that you can't resolve the PHP's trait feature with bouml (it's easier with your tips), it's just that would be nice if it's out of the box :)

Thanks!
Federico.
fbernoldi
 
Posts: 40
Joined: Mon 10 Jul 2017 14:15

Re: PHP Traits

Postby Bruno Pagès » Wed 6 Dec 2017 11:06

The thing is, unless you add type a stereotype on those relations, or a stereotype on the class you are changing to trait on the php definition, when you are looking it on a bouml's class diagram it's shown like a regular relation and a regular class and you can't tell that it's a trait.

yes, just note that
  • you are not limited to the stereotypes I deliver and you are able to add yours including in the default stereotypes list through the settings
  • you are also able to define 'true' stereotype e.g. in profile, and to associate plug-out to them to perform automatic modifications, for instance to set class/relation definition to be the expected ones for traits (but of course even this allows to have the right code generation the reverse ignore them)

why not stereotype a class to trait and that bouml do "the same" there.

I never use Php, I did not know that traits exist, I did not see them when I introduced Php in Bouml, or may be they did not yet exist when I did
ImageAuthor of Bouml
Bruno Pagès
 
Posts: 603
Joined: Mon 20 Feb 2012 09:23
Location: France

Re: PHP Traits

Postby Bruno Pagès » Tue 26 Dec 2017 13:50

Hello,

trait managed in 7.3 adding the new stereotype trait and a special behaviour for the generalizations to classes stereotyped trait whose produce use forms

Let's note it is possible to generate/reverse/roundtrip forms use inheriting several traits (like use A,B;) including insteadof or as forms, please reverse these cases to see how to model them.

The default definition for a generalization to a trait is ${type} producing two spaces followed by the use form, if you want an other indentation edit the generalization to replace ${type} by the expected amount of spaces/tabs followed by the full form to generate (<spaces/tabs>use ${type};)

Kind regards,
Bruno
ImageAuthor of Bouml
Bruno Pagès
 
Posts: 603
Joined: Mon 20 Feb 2012 09:23
Location: France

Re: PHP Traits

Postby fbernoldi » Thu 28 Dec 2017 11:55

Hi Bruno,

Thank you!

Let's note it is possible to generate/reverse/roundtrip forms use inheriting several traits (like use A,B;) including insteadof or as forms, please reverse these cases to see how to model them.


What did you meant with this?

Regards,
Federico.
fbernoldi
 
Posts: 40
Joined: Mon 10 Jul 2017 14:15

Re: PHP Traits

Postby Bruno Pagès » Thu 28 Dec 2017 12:23

By default when a class inherits several traits the generated code contains one use per inherited trait, but Bouml is also able to reverse/roundtrip/generate something like that :

Code: Select all
class Talker {
  use A , B {
    B :: smallTalk insteadof A ;
    A :: bigTalk insteadof B ;
  }
}


of course in that case there are still two generalizations, but there are supported by only one use, more the {...}

Just reverse the full "Example #5 Conflict Resolution" from http://php.net/manual/en/language.oop5.traits.php and look at the two generalization definitions in Php
ImageAuthor of Bouml
Bruno Pagès
 
Posts: 603
Joined: Mon 20 Feb 2012 09:23
Location: France


Return to Change requests / Demandes d'évolution

Who is online

Users browsing this forum: No registered users and 0 guests

cron