php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71275 Bad method called on cloning an object having a trait
Submitted: 2016-01-04 16:09 UTC Modified: 2016-01-05 14:00 UTC
From: patrick dot barroca at gmail dot com Assigned: bwoebi (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.0.1 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: patrick dot barroca at gmail dot com
New email:
PHP Version: OS:

 

 [2016-01-04 16:09 UTC] patrick dot barroca at gmail dot com
Description:
------------
I have a trait for translations that contains a public function _().
I clone an object that use this trait.

Expected : Object or trait method __clone() is called by PHP if present.
Actual : __clone() method is not called, _() method is called by PHP instead

This does not occur when the class itself have a _() method, this seems to be related to traits.



Test script:
---------------
<?php

trait MyTrait {
  public function _() {
    throw new RuntimeException('Should not be called');
  }
}


class MyClass {
  use MyTrait;

  public function __clone() {
    echo 'I\'m working hard to clone';
  }
}


$instance = new MyClass();
clone $instance;

Expected result:
----------------
should display "I'm working hard to clone"

Actual result:
--------------
PHP Fatal error:  Uncaught RuntimeException: Should not be called in [...]clone_poc.php:5
Stack trace:
#0 /home/pbarroca/workspace/clone_poc.php(20): MyClass->_()
#1 {main}
  thrown in /home/pbarroca/workspace/clone_poc.php on line 5

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-05 14:00 UTC] bwoebi@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: bwoebi
 [2016-01-05 14:00 UTC] bwoebi@php.net
Yes, traits only, in case the method name matches any prefix of that length of a magic method...
 [2016-01-05 15:28 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=654c8aedd12c217987191c83e6e93c8e756c1a6e
Log: Fixed bug #71275 (Bad method called on cloning an object having a trait)
 [2016-01-05 15:28 UTC] bwoebi@php.net
-Status: Assigned +Status: Closed
 [2016-01-05 15:28 UTC] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=654c8aedd12c217987191c83e6e93c8e756c1a6e
Log: Fixed bug #71275 (Bad method called on cloning an object having a trait)
 [2016-07-20 11:34 UTC] davey@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=654c8aedd12c217987191c83e6e93c8e756c1a6e
Log: Fixed bug #71275 (Bad method called on cloning an object having a trait)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC