Edit report at https://bugs.php.net/bug.php?id=65039&edit=1

 ID:                 65039
 Updated by:         larue...@php.net
 Reported by:        easteregg at verfriemelt dot org
 Summary:            wrong late static binding in traits
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.4.16
 Block user comment: N
 Private report:     N

 New Comment:

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));
~


Previous Comments:
------------------------------------------------------------------------
[2013-06-14 11:11:34] 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]


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65039&edit=1

Reply via email to