PHP use and namespaces

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 use and namespaces

Postby fbernoldi » Tue 3 Oct 2017 14:05

Hi,

In PHP you can do something like:

Code: Select all
<?php

namespace Test;

use \SomeNamespace\SomeClass;
use \SomeVeryLargeAnoyingNamespace\SomeVeryLargeAnoyingClass as SVLAC;

class Test {

    public function dependency() {
        $s = new SomeClass;
        $svlac = new SVLAC;
        $svlac2 = new \SomeVeryLargeAnoyingNamespace\SomeVeryLargeAnoyingClass();
    }

    public function someNiceMethod(SVLAC $hello) {
        // something php 7 friendly
    }

    /**
     * @param SVLAC $hello some hi hi.  (@param ${t0} ${p0} ...)
     */
    public function someNiceMethodUntyped($hello) {
        // something php 5 friendly
    }

    /**
     * @param SVLAC $hello some hi hi. (@param ${t0} ${p0} ...)
     * @return SVLAC (${type})
     */
    public function someNiceMethodReturning(SVLAC $hello) : SVLAC {
        // something php 7.1 friendly
        return $hello;
    }

    /**
     * @param \SomeVeryLargeAnoyingNamespace\SomeVeryLargeAnoyingClass $hello Some hello thing
     */
    public function someUglyMethod(\SomeVeryLargeAnoyingNamespace\SomeVeryLargeAnoyingClass $hello) {
        // something else
    }

}


Currently there is a force namespace prefix but I can't find a way to use it this way, it's quite demanding to add the uses you need to artifacts manually.

So the feature request would be that the current "import" dependency generates a PHP use statement at least so you can use the alias "SomeClass" instead of "\SomeNamespace\SomeClass", I don't know how to solve the "SVLAC" alias case.

And for the methods like "someNiceMethod" to generate a PHP use statement automatically and use it when the force namespace prefix is not set (for example).

These features would be very useful for big projects with a lot of namespaces!

Thanks,
Federico.
fbernoldi
 
Posts: 22
Joined: Mon 10 Jul 2017 13:15

Re: PHP use and namespaces

Postby Bruno Pagès » Thu 5 Oct 2017 16:05

Hello,

About force namespace prefix, supposing the class A and B are in same package P and A extends B :
  • if force namespace prefix is not set the generated code is class A extends B ...
  • if force namespace prefix is set the generated code is class A extends \P\B ...

Yes, currently the uses have to be added to artifacts manually.

The dependencies are already used to generate code, they allow to generate additional require_once whatever their stereotypes. Let's note the require_once are automatically generated, for instance if A has an attribute of type B and A generated in A.php and B generated in B.php, the code generated in A.php will contain require_once 'B.php'; even it is not explicitly required through a dependency

I can modify the dependency management between two classes, for instance for a dependency A - - -> B :
  • if the stereotype is use the generated code in A.php will be a use B; (if necessary with the namespace where B is)
  • if the stereotype is use-as FOO the generated code will be a use B as FOO; (if necessary with the namespace where B is)

So the feature request would be that the current "import" dependency generates a PHP use statement ...

And for the methods like "someNiceMethod" to generate a PHP use statement automatically and use it when the force namespace prefix is not set (for example).


I don't understand, the same use form is generated in both cases, when there is a dependency for the first case, without dependency in the second case, what is the interest to have a dependency ?
ImageAuthor of Bouml
Bruno Pagès
 
Posts: 449
Joined: Mon 20 Feb 2012 08:23
Location: France

Re: PHP use and namespaces

Postby fbernoldi » Mon 9 Oct 2017 19:40

Hi Bruno,

Bruno Pagès wrote:Hello,

About force namespace prefix, supposing the class A and B are in same package P and A extends B :
  • if force namespace prefix is not set the generated code is class A extends B ...
  • if force namespace prefix is set the generated code is class A extends \P\B ...

Yes, currently the uses have to be added to artifacts manually.

The dependencies are already used to generate code, they allow to generate additional require_once whatever their stereotypes. Let's note the require_once are automatically generated, for instance if A has an attribute of type B and A generated in A.php and B generated in B.php, the code generated in A.php will contain require_once 'B.php'; even it is not explicitly required through a dependency

I can modify the dependency management between two classes, for instance for a dependency A - - -> B :
  • if the stereotype is use the generated code in A.php will be a use B; (if necessary with the namespace where B is)
  • if the stereotype is use-as FOO the generated code will be a use B as FOO; (if necessary with the namespace where B is)

So the feature request would be that the current "import" dependency generates a PHP use statement ...


Great!

Bruno Pagès wrote:
And for the methods like "someNiceMethod" to generate a PHP use statement automatically and use it when the force namespace prefix is not set (for example).


I don't understand, the same use form is generated in both cases, when there is a dependency for the first case, without dependency in the second case, what is the interest to have a dependency ?


The goal is that BoUML generates the short class names according to the uses declared, for dependencies, associations and method signature.

i.e. for an association:

Code: Select all
<php

use SNamespace\SClass;

class HelloClass {

    /**
     * @var SClass this could be an association with stereotype "use" with return type SNamespace\SClass using ${type} in documentation.
     */
    private $asociated_instance;

}



For a method signature:

Code: Select all
<php

use SNamespace\SClass;

class HelloClass {

    /**
     * @param SClass $sclass some typed ${t0} param
     * @return SClass some return ${type}
     */
    public function hello (SClass $sclass) {
        // ...
        return $sclass;
    }

}



I hope I explained better with separated examples.

Regards,
Federico.
fbernoldi
 
Posts: 22
Joined: Mon 10 Jul 2017 13:15


Return to Change requests / Demandes d'évolution

Who is online

Users browsing this forum: No registered users and 2 guests