From:             alt dot e-mail at gmx dot de
Operating system: Irrelevant
PHP version:      5.3.0beta1
PHP Bug Type:     Feature/Change Request
Bug description:  Cloning non-object does not throw exception?

Description:
------------
Since v5.2.5, cloning a non-object results in a fatal error. In  my
opinion that is the wrong behaviour!

It's not really a fatal error (as in DEATH, CORE-DUMP, END-OF-THE-WORLD)
to clone a non-object. It's really just an exception (as in OOPS, HELP?,
DO-WHAT-NOW?), that should be user-catchable.

A practical example:

I have an array where the values are strings or objects that can be cast
to string with "__toString". If I want to create a copy of that array, I
also need to clone the objects. 

Reproduce code:
---------------
<?php
class Color {
    // assume their values are between 0.0 and 1.0
    private $r, $g, $b;
    public function toString() {
        return sprintf("#%02X%02X%02X",
        (int)$this->r*255, (int)$this->g*255, (int)$this->b*255);
    }
}
// somewhere in your code you create an array of colors
$colors = array(
    "#FFF000", new Color( 0, 0, 1 ), // and many more
);
// somewhere else you want to copy that array
// this is the current way
$newColors = array();
foreach ( $colors as $color ) {
    if ( is_object( $color ) {
        try { $newColors[] = clone $color; }
        // do nothing, error in __clone-method or whatever
        catch ( MySomethingException $e ) {}
    }
    else { $newColors[] = $color; }
}
// this is the way it should be
$newColors = array();
foreach ( $colors as $color ) {
    try { $newColors[] = clone $color; }
    catch ( TypeException/*or something*/ $e ) { $newColors[] = $color; }
}


-- 
Edit bug report at http://bugs.php.net/?id=47345&edit=1
-- 
Try a CVS snapshot (PHP 5.2):        
http://bugs.php.net/fix.php?id=47345&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):        
http://bugs.php.net/fix.php?id=47345&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):        
http://bugs.php.net/fix.php?id=47345&r=trysnapshot60
Fixed in CVS:                        
http://bugs.php.net/fix.php?id=47345&r=fixedcvs
Fixed in CVS and need be documented: 
http://bugs.php.net/fix.php?id=47345&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=47345&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=47345&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=47345&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=47345&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=47345&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=47345&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=47345&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=47345&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=47345&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=47345&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=47345&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=47345&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=47345&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=47345&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=47345&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=47345&r=mysqlcfg

Reply via email to