# Reflection, Traits, Aliasing

- Thread: https://externals.io/message/54190
- Original archive: https://news-web.php.net/php.internals/54190
- Started by johannes@schlueters.de on 2011-07-25T17:21:49+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54190 — johannes@schlueters.de — 2011-07-25T17:21:49+00:00

URL: https://externals.io/message/54190#54190

Stefan,

I recently tried to finalize reflection support for traits. Given code
like

<?php
trait T1 {
    public function t1() {}
    public function ta() {}
}


trait T2 {
    public function t1() {}
    public function tb() {}
}

class C {
    use T1, T2 {
        T2::t1 insteadof T1;
        T1::t1 as tc;
    }
}
?>

Now I use reflection on this:

$rc = new ReflectionClass('C');
print_r($rc->getTraitAliases());

Array
(
    [tc] => T1::t1
)

So far so nice but I'm missing the information where C::t1() is coming
from. In the reflection code I'm currently iterating over
ce->trait_aliases and can't find where I can get the information from.
Actually I'd be even interested in getting all important methods and
their origin. Stefan, do you know where I can find the information or
would we have to store it additionally?

johannes

---

## #54193 — Stefan Marr — 2011-07-25T21:05:27+00:00

URL: https://externals.io/message/54190#54193 · In reply to: #54190

Hi Johannes:

2011/7/25 Johannes Schlüter <johannes@schlueters.de>:

> Now I use reflection on this:
>
> $rc = new ReflectionClass('C');
> print_r($rc->getTraitAliases());
>
> Array
> (
>    [tc] => T1::t1
> )

Great, that is nice.


> So far so nice but I'm missing the information where C::t1() is coming
> from. In the reflection code I'm currently iterating over
> ce->trait_aliases and can't find where I can get the information from.
> Actually I'd be even interested in getting all important methods and
> their origin. Stefan, do you know where I can find the information or
> would we have to store it additionally?
The functions do not store that information, so there are basically
only two approaches, I see:

Either, factoring out the code which is doing the flattening, conflict
resolution, and class composition into something which could be easily
shared by both the reflection and the engine, and then re-doing it on
demand in the reflection,
or we extend zend_function by an origin pointer.

Not sure what the better tradeoff is general memory overhead vs.
cache-able pay-as-you-go overhead.

Best regards
Stefan

---

## #54471 — Keloran — 2011-08-08T14:52:37+00:00

URL: https://externals.io/message/54190#54471

There seems to be a bug in traits that if you use any of the GLOBAL vars it
segfaults

e.g.
<?php
trait tester {
  function getStuff($a = null) {
    if ($a) {
      if (isset($_COOKIE[$a])) {
         return $_COOKIE[$a];
      }
    }
    return false;
  }
}

<?php
include "tester.php";
class beep {
  use tester;

  function beeper() {
    return $this->getStuff("tester");
  }
}

that causes a segfault

change the trait to
<?php
trait tester {
  function getStuff($a) {
    if ($a) {
      if (isset($_COOKIE[$a])) {
         return $_COOKIE[$a];
      }
    }
    return false;
  }
}

and now you get a zend_mm_corrupted,

this is in 5.4.0-beta1 compiled at 23:59 on 7th



> 
> > […]
>
> Is it really worth any overhead here at all? This information is still
> there, you just have to work at it a bit. You know which traits are
> being used for class C. T1 and T2. You can look at T1 and T2 and see
> they both provide t1 methods, but you see from the list of aliases on C
> that T1::t1 was aliased to tc, therefore C::t1 must come from T2 since
> the trait is used in C and it isn't aliased. I don't think we need
> anything else here.
>
> -Rasmus

---

## #54492 — Joey Smith — 2011-08-09T17:10:36+00:00

URL: https://externals.io/message/54190#54492 · In reply to: #54471

> There seems to be a bug in traits that if you use any of the GLOBAL vars it
> segfaults

<snip>

I'm not sure it's clear from Keloran's code example here, so I thought I'd point
out that the problem only seems to happen if you include the trait definition
from an external file.

---

## #54493 — Stefan Marr — 2011-08-09T17:12:34+00:00

URL: https://externals.io/message/54190#54493 · In reply to: #54492

Hi:


> 
> > […]
> 
> <snip>
> 
> I'm not sure it's clear from Keloran's code example here, so I thought I'd point
> out that the problem only seems to happen if you include the trait definition
> from an external file.

Thanks Keloran and Joey, it is on my todo list, and I will look into it over the weekend.

Best regards
Stefan

-- 
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax:   +32 2 629 3525
