php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54441 Traits - Visibility on alias names
Submitted: 2011-04-01 12:47 UTC Modified: 2011-11-18 13:53 UTC
From: php-svn at helio dot arq dot br Assigned: gron (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: trunk-SVN-2011-04-01 (snap) OS: Linux
Private report: No CVE-ID: None
 [2011-04-01 12:47 UTC] php-svn at helio dot arq dot br
Description:
------------
I'm trying Traits in php-trunk version.
In a give moment, I tried to change visibility in a alias trait's method.

Test script:
---------------
trait Foo {
  public static function lol() {
    echo 'Foo!';                                                            
  }
}

class Boo {
  use Foo {
    Foo::lol as dontKnow; 
    dontKnow as protected;
  }
}
$boo = new Boo();
$boo->dontKnow();

Expected result:
----------------
Fatal error: Call to protected method Boo::dontKnow() from context '' in %s on line %d

Actual result:
--------------
Foo!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-02 18:05 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: gron
 [2011-04-02 18:14 UTC] felipe@php.net
-Package: Unknown/Other Function +Package: Scripting Engine problem
 [2011-04-02 18:14 UTC] felipe@php.net
According to the actual grammar and implementation, the right way to do this is using:

Foo::lol as protected dontKnow;


Stefan Marr, is supposed to work the code as the bug reporter tried it?
 [2011-04-03 15:30 UTC] php at stefan-marr dot de
Interesting question.

I suppose it could be legal (but obviously it is currently not implemented).
It looks kind of ugly to me, since it is less concise.
But well, all the traits use definitions should be declarative and orderless, so 
if it is possible to do something with two separate declarations then people 
might expect that it works.

Not sure whether we want that... 
But anyway, a good catch, thanks.
Stefan
 [2011-07-22 13:33 UTC] me at mikestowe dot com
class Boo {
  use Foo {
    Foo::lol as dontKnow; 
    dontKnow as protected;
  }
}

To me this would do the following:
Use "Foo", set "Foo:lol" as "dontKnow", and then set "dontKnow" as "protected"

Meaning that I should be able to call $boo->lol, $boo->dontKnow, or $boo->protected to accomplish the same thing.

Otherwise it should throw a method does not exist error if the "as" is supposed to rename the function.
 [2011-07-23 08:01 UTC] gron@php.net
Hm, today I feel like it is not a good idea to allow

use Foo {
  Foo::lol as dontKnow; 
  dontKnow as protected;
}

Because we will end up with chains like this:

use Foo {
  Foo::lol as dontKnow; 
  dontKnow as foo;
  foo as bar;
  bar as baz;
}

And I do not see the use case for it, while I see readability problems.

This is all equivalent to:

use Foo {
  Foo::lol as dontKnow; 
  Foo::lol as foo;
  Foo::lol as bar;
  Foo::lol as baz;
}

Which IMHO is a lot clearer. Every level of indirection is another level of added complexity, and I do not see a relevant use case at the moment.

So, I will regard this as something that should not work.
Not sure yet how to design it properly. Will look at it.
 [2011-11-18 13:48 UTC] gron@php.net
Automatic comment from SVN on behalf of gron
Revision: http://svn.php.net/viewvc/?view=revision&revision=319483
Log: Fixes Bug #54441 (Handling of changing modifiers on a trait alias)
# this now results also in a compilation error, since it would open the door for inconsistencies, and violates the DRY principle.
 [2011-11-18 13:53 UTC] gron@php.net
-Status: Assigned +Status: Closed
 [2011-11-18 13:53 UTC] gron@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed in SVN rev 319483.

These kind of two-step declarations are now disallowed and cause a compilation 
error.

The reason is that it invites inconsistencies like:

Foo::lol as dontKnow public; 
dontKnow as protected;

Possible inconsistencies, as well as the 'dont repeat yourself' principle are 
the reasons to disallow it completely.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 07:01:29 2024 UTC