17

Are traits in php5.4 subject to autoloading?

I've not yet got an environment to test in, but I can't see any mention of it on __autoload at php.net or on the traits page, but it seems traits behave like classes in some regards.

Has anyone tried this yet?

UPDATE:

I found a request here: https://bugs.php.net/bug.php?id=61265 (2012-03-03 13:10 UTC)

that seems to suggest it does work, but not explicitly. Can anyone confirm that a straight __autoload() will be called for a missing trait?

UPDATE: Confirmed - it works as expected - __autoload will fetch traits, although getting php5.4 to work first time seems to be bigger challenge.

Thanks, MyStream

hakre
  • 193,403
  • 52
  • 435
  • 836
MyStream
  • 2,533
  • 1
  • 16
  • 33

2 Answers2

56

According to the manual, the trait_exists() function takes a boolean as second parameter, that is related to autoloading; which seems to indicate that traits and autoload are not two incompatible ideas.

In addition, if you take a look at the source-code of that trait_exists() function, you'll see a section of code, conditioned by that second parameter, that looks quite similar to what you can see in the source-code of class_exists().
So, I'd say a second time that traits and autoload are not incompatible ideas ;-)

(I don't have PHP 5.4 installed on my current computer, so I cannot test by myself -- but, looking at the code...)



[edit] OK, I've just compiled PHP 5.4.3, the current stable version:

$ /usr/local/bin/php --version
PHP 5.4.3 (cli) (built: May 17 2012 21:11:42)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

Let's try the following short portion of code, which is saved as temp-2.php, and tries to use a trait that is not declared in this file:

spl_autoload_register(function ($name) {
    echo "AUTOLOAD :: ";
    var_dump($name);
});

class MyClass {
    use MyTrait;   // Let's try to use a trait that is not declared in this file
}

$obj = new MyClass();
$obj->plop();

Basically, if autoloading works for traits, "AUTOLOAD" and the name of my trait should be displayed.

So, let's try executing that portion of code -- and here is the result I get:

$ /usr/local/bin/php ./temp-2.php
AUTOLOAD :: string(7) "MyTrait"

Fatal error: Trait 'MyTrait' not found in /.../temp-2.php on line 13

So, the autoloading function (here, an anonymous one -- but that doesn't change a thing) is called...
... which means that traits are, with PHP 5.4.3, subject to autoloading.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

Confirmed as well:

PHP 5.4.3-1~dotdeb.0 (cli) (built: May  8 2012 20:49:26)

Fatal error: Trait 'x' not found in /.../index.php on line 12

They are treated as classes because they are implemented as (modiffied?) classes.

For example they currently have a residual behaviour from classes: they can own properties.

In theory they shouldn't and you shouldn't rely on them having this ability. In the future it will probably be phased out.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
  • Thank you for that - very useful answer and advice. +1 It will be very interesting to see how it develops. – MyStream May 17 '12 at 19:28
  • IMO it's a very bad/clumsy syntax. Allowing properties in traits (if it continues). B.asically allows for "full" multiple inheritance. I had been excited to test this feature just like I was about namespaces back @php5.3, and again disappointment. – Mihai Stancu May 17 '12 at 19:49
  • Also in php5.4 we have direct function return value resolution: explode(' ', 'Mihai Stancu')[1] == 'Stancu', and array syntax ['key' => 'value']. – Mihai Stancu May 17 '12 at 19:54
  • I don't like namespaces or their php implementation, but functional traits look very useful to me at the moment. – MyStream May 17 '12 at 23:26
  • Yeah, but as opposed to the old "mix-ins" we used to work with which could be dynamically loaded, this is pretty weak. You know the ones when you had to define an array of "mixed-in" class-names or class_names=>method_names? – Mihai Stancu May 17 '12 at 23:38
  • 2
    Yeah - I remember :) - the same concept has made its way into CSS (SASS et al) I'm not opposed to the idea - mostly implementation, but happy that Traits provide a way to ease bloat in PHP without enforcing unusual inheritance patterns of namespaces or interfaces for the sake of additional functionality. – MyStream May 18 '12 at 17:41