php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65039 wrong late static binding in traits
Submitted: 2013-06-14 11:11 UTC Modified: 2013-06-14 15:15 UTC
From: easteregg at verfriemelt dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.16 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: easteregg at verfriemelt dot org
New email:
PHP Version: OS:

 

 [2013-06-14 11:11 UTC] easteregg at verfriemelt dot org
Description:
------------
Hi

when i use a trait with late static bindings i get the wrong bindings, when i use 
an object A with a trait T and then inherit this trait to a new object B and call 
the trait T from within that object B, i will get an late binding to object A 
instead of object B as expected

where as when use the traitmethod within the object A and inherit this for object 
B and then call it, the static binding are bound to object b as expected

Test script:
---------------
<?php 
    trait getObjectById { 

        public static $_cache = []; 
         
        public static function get($id,$autoload = true) { 
            if (!isset(static::$_cache[$id])) { 
                static::$_cache[$id] = new static($id, $autoload); 
            } 

            return static::$_cache[$id]; 
        } 
    } 
     
    class User { use getObjectById; } 
     
    class User2 extends User { } 
     
    //class User3 extends User { use getObjectById; }
    // var_dump(User3::get(1)) would return the right object cause the trait is overwritten
     
    var_dump(User::get(1)); 
    var_dump(User2::get(1));

Expected result:
----------------
object(User)[1]
object(User2)[1]

Actual result:
--------------
object(User)[1]
object(User)[1]

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-14 15:15 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2013-06-14 15:15 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

see following example:
<?php
trait getObjectById {

    public static $_cache = [];

    public static function get($id,$autoload = true) {
        if (!isset(static::$_cache[$id])) {
            static::$_cache[$id] = new static($id, $autoload);
        }

        return static::$_cache[$id];
    }
}

class User { use getObjectById; }

class User2 extends User {
    public static $_cache = []; // difference here 
}

//class User3 extends User { use getObjectById; }
// var_dump(User3::get(1)) would return the right object cause the trait is 
overwritten

var_dump(User::get(1));
var_dump(User2::get(1));
~
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 18:01:28 2025 UTC