Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:37:16+00:00 2026-06-04T00:37:16+00:00

Are traits in php5.4 subject to autoloading? I’ve not yet got an environment to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:37:18+00:00Added an answer on June 4, 2026 at 12:37 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python EPD Traits I can keep variables that depend on others up to
I've got a one to many table with ids and traits for those users.
Traits classes can be defined to check if a C++ class has a member
I understand that C++ Traits are compile time properties that can be used to
I want to have classes that can mix only specified traits: class Peter extends
I needed to use the std::is_assignable type traits, which happens not to be available
Can someone please explain traits in Scala? What are the advantages of traits over
There are these in <type_traits> : is_pointer<> is_function<> is_member_function_pointer<> But not this: is_function_pointer<> Why
Scala has a number of traits that you can use as type classes, for
I see that the new planned features for PHP 5.4 are: traits, array dereferencing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.