php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55128 SplFixedArray::fromArray() does not use extended class
Submitted: 2011-07-04 09:50 UTC Modified: 2013-02-05 16:38 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: ar at ez dot no Assigned:
Status: Wont fix Package: SPL related
PHP Version: 5.3.6 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ar at ez dot no
New email:
PHP Version: OS:

 

 [2011-07-04 09:50 UTC] ar at ez dot no
Description:
------------
It would be nice if SplFixedArray::fromArray() returned the child class instance 
instead of SplFixedArray instance.

Test script:
---------------
class FixedCollection extends SplFixedArray {
    // impl
}

$collection = FixedCollection::fromArray( array( 22, 42 ) );

if ( 'FixedCollection' !== get_class( $collection ) )
    trigger_error( "fromArray() does not use 'new static'" )



Expected result:
----------------
I would have expected that function returned an instance of FixedCollection.


Patches

php-5-3-splfixedarray-fromarray-should-return-call-scope-instance (last revision 2011-07-26 09:57 UTC) by laruence@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-26 05:35 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5-3-splfixedarray-fromarray-should-return-call-scope-instance
Revision:   1311658555
URL:        https://bugs.php.net/patch-display.php?bug=55128&patch=php-5-3-splfixedarray-fromarray-should-return-call-scope-instance&revision=1311658555
 [2011-07-26 05:36 UTC] laruence@php.net
-Status: Open +Status: Verified
 [2011-07-26 05:36 UTC] laruence@php.net
-Status: Verified +Status: Analyzed
 [2011-07-26 08:36 UTC] cataphract@php.net
What if FixedCollection has a constructor?
 [2011-07-26 09:49 UTC] laruence@php.net
hmm, that's a really problem, if we want to provide this feature, we should make fixedarray::__construct final.
 [2011-07-26 09:57 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5-3-splfixedarray-fromarray-should-return-call-scope-instance
Revision:   1311674267
URL:        https://bugs.php.net/patch-display.php?bug=55128&patch=php-5-3-splfixedarray-fromarray-should-return-call-scope-instance&revision=1311674267
 [2011-07-26 10:10 UTC] laruence@php.net
After much thought, I think there maybe no reason for fixedarray extended class  to define it's own constructor, since we provide fixedarrary::__construct and fromArray already, and extended class should only get an instance through calling them.

so I think we can make fixedarray::__construct final to make this feature works.
 [2011-07-27 05:40 UTC] ar at ez dot no
that sounds like potentially breaking code. cant see the patch from here(surfing 
on kindle while on vacation), but isn`t it something like "new static()" that can 
be used?
 [2013-02-05 16:38 UTC] levim@php.net
The solution is to reimplement it in your subclass. Static functions aren't 
inherited in the same way.  In  my opinion fromArray should never have existed, 
but it's too late for that.
 [2013-02-05 16:38 UTC] levim@php.net
-Status: Analyzed +Status: Wont fix
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC