Search code examples
phpuml

How to visualize an array of variable references in UML Class diagram


I'm struggling creating a UML class diagram from PHP source code.

I have the following code:

   abstract class DataSource
{
    public function toReferences():array
    {
        $fields = [];
        $keys = $this->getPropertyNames();
        foreach( $keys as $key ){
            $fields[] = &$this->$key;
        }
        return $fields;
    }

    public function getPropertyNames():array
    {
        return array_keys( get_object_vars( $this ) );
    }
}

The function toReferences returns an array (collection) of property references.
Every property can be of a different datatype.
I'm not sure if I could use the well-known pseudotype "mixed" from the php documentation.
If so would this be a correct UML notation?

+toReferences:&mixed[]

Solution

  • An array of unbounded size indeed usually implements in a programming language an UML property with a multiplicity *

    If you would know what type or super type will be used, you should in your UML model refer to the known type as specifically as possible, regardless of the fact that the implementation language has not a strong typing. However, if your design intent is really to have any possible type of that language, you could refer to that common supertype if the language has one. In PHP it is indeed mixed. Note that standard UML does not know language specific type, but you could use them with a language specific profile.

    So text notation for your function would be:

    +toReferences(): mixed[*]
    

    There is nothing equivalent in UML to the & reference. UML has a reference semantic for Classes, and value semantic for DataTypes, but the difference in UML is very subtle and ignored by many modelers. If this is important for your modelling (e.g. if you use UML to document your code in detail, instead of using it to express the underlying design), you could consider in your language profile define a stereotype «reference» that you would put in front of the type.