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
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Tue Mar 19 03:01:29 2024 UTC