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 6227047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:08:41+00:00 2026-05-24T09:08:41+00:00

While this question is somewhat language agnostic ( agnostic as far as OOP languages

  • 0

While this question is somewhat language agnostic (agnostic as far as OOP languages that support Traits) I’ve been tinkering with the nightly builds of PHP 5.4a, and came across an odd scenario. I can’t seem to get my install to run anymore, but that’s another story.

Given the following snippet:

trait MyTrait
{

    public function myMethod(self $object)
    {
        var_dump($object);
    }

}

class MyClass
{

    use MyTrait;

}

$myObject = new MyClass();
$myObject->myMethod('foobar'); // <-- here

What should happen? I would hope for an error, indicating $object needs to be an instance of MyClass.

When trait methods are copied into a use-ing class, are they copied verbatim, as to resolve class inheritance references like these? Is this the intended functionality of a Trait? (I’ve not worked with another language that supported them)

  • 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-05-24T09:08:43+00:00Added an answer on May 24, 2026 at 9:08 am

    Well, I’ve confirmed it is in fact as I had hoped for and expected:

    class MyClass
    {
    
        use MyTrait;
    
    }
    
    $myObject = new MyClass();
    
    $myObject->myMethod($myObject); // ok
    
    $myObject->myMethod('foobar'); // Catchable fatal error, argument must be instance etc
    

    So, good news for all then.

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

Sidebar

Related Questions

G'day, Edit: While this question covers a situation that can arise in programming a
While experimenting with this question on collections in Spring.NET , I discovered that Spring
While trying to answer this question I found that the code int* p =
This has been bugging me for a while and I'm hoping that one of
Somewhat of an academic question, but I ran into this while writing some unit
My question, while at first somewhat similar to this one , seems to be
Somewhat inspired by this question about a graphical programming environment. I don't think that
While thinking about this question and conversing with the participants, the idea came up
While I understand this question is fairly vague since I'm not giving you all
Unlike this question: Linker Error while building application using Boost Asio in Visual Studio

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.