php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63629 New constant __ALIAS__ for trait functions to know how they were called
Submitted: 2012-11-27 18:52 UTC Modified: 2014-02-06 14:46 UTC
Votes:6
Avg. Score:3.7 ± 1.4
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:0 (0.0%)
From: spamfreedave-zend at yahoo dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 5.4.9 OS: Mac OSX
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-11-27 18:52 UTC] spamfreedave-zend at yahoo dot com
Description:
------------
I would like my trait functions to know what alias (if any) was used to call them.

The idea came from discovering that you can give a trait function multiple aliases (see Test Script).

It occurs to me that you could have some interesting dynamic behavior if the trait function was able to determine what name (alias) was used to call it.

If possible, a new constant __ALIAS__ could be created to store this value.

Thank you for your consideration.

Test script:
---------------
<?php
trait TestTrait
{
	public function test() { print __FUNCTION__ . ', ' . __ALIAS__  . "\n"; }
}
class TestClass
{
	use TestTrait { test as test2; test as test1; }
}
$c = new TestClass();
$c->test1();
$c->test2();


Expected result:
----------------
test, test1
test, test2


Actual result:
--------------
test, __ALIAS__
test, __ALIAS__


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-28 09:50 UTC] laruence@php.net
could you explain a little more: what i " some interesting dynamic behavior" ?

I mean , the real use case.
 [2012-11-28 09:50 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2012-11-28 09:55 UTC] gron@php.net
What about:

  $backtrace=debug_backtrace();
  echo $backtrace[0]['function'];

That seems to be also the common solution for getting the name of the calling 
function.
 [2013-02-18 00:36 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 [2014-02-06 14:17 UTC] opitz at pluspol-interactive dot de
Following Scenario:

trait TestTrait
{
  protected $var = null;
  public function setMethod($value) {
    $this->var = $value;
  }
  public function getMethod() {
    return $this->var;
  }
}

class TestClass {
  use TestTrait {
    setMethod as setMethod1;
    setMethod as setMethod2;
    getMethod as getMethod1;
    getMethod as getMethod2;
  }
}

$class = new TestClass();
$class->setMethod1('A');
$class->setMethod2('B');
echo $class->getMethod1();
echo $class->getMethod2();

This will output "BB" instead of "AB" With __ALIAS__ we could use different poperties per Trait usage in the getter/setter.
A better aproch would be usage of templates, so the setter assignment can depend on trait inlusion: Like

trait TestTrait
{
  protected $<MyTraitVar> = null;
  public function setMethod($value) {
    $this-><MyTraitVar> = $value;
  }
  public function getMethod() {
    return $this-><MyTraitVar>;
  }
}

class TestClass {
  use TestTrait as TestTrait1, TestTrait as TestTrait2 {
    TestTrait1::<MyTraitVar> as MyVar1;
    TestTrait2::<MyTraitVar> as MyVar2;
    TestTrait1::setMethod as setMethod1;
    TestTrait2::setMethod as setMethod2;
    TestTrait1::getMethod as getMethod1;
    TestTrait2::getMethod as getMethod2;
  }
}
 [2014-02-06 14:41 UTC] gron@php.net
Perhaps a naive question, but if you want a function to behave differently based on the name it has, why don't you just provide distinct functions then?
 [2014-02-06 14:46 UTC] gron@php.net
Sorry, the last comment was with regard to the original question.
Looking at the example code of 'opitz at pluspol-interactive dot de', it looks to me code generation, or templates would be much more useful here. I would not want to advocate for such usage by introducing another magic constant.

However, I also can see a point in having a magic constant that allows to define unique names for properties within a trait. But, that might be something else entirely.
 [2014-02-06 15:43 UTC] opitz at pluspol-interactive dot de
Templating would be very interesting for traits but also for things like
$myConfigArray = \ArrayObject<\Project\Config\My> to have an ArrayObject where you only can add Objects of this instance. ;)
 [2019-01-29 21:03 UTC] php at nerd dot guru
I'm not even sure this will bump the issue, but I'd still really like to see this resolved in either 7.4 or 8.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 01:01:30 2024 UTC