Ticket #61 (closed enhancement: fixed)

Opened 1 year ago

Last modified 11 months ago

Create a stubClonable interface

Reported by: mikey Assigned to: mikey
Priority: major Milestone:
Component: core Version:
Keywords: Cc:

Description

As already described in ticket #54 PHP does not offer any possibility to check whether a class is clonable or not. It goes even so far that it throws an E_FATAL if you try to clone an instance of the ReflectionClass. To solve this issue an interface net.stubbles.stubClonable will be introduced. Furthermore only classes implementing this interface will be cloned, if they do not a reference copy will be made. See stubAbstractAnnotation::__clone() for a concrete use case.

Change History

06/05/07 23:38:12 changed by schst

I found a way to detect, whether a class is cloneable:

$clazzClazz = new ReflectionClass('ReflectionClass');
$method = $clazzClazz->getMethod('__clone');
if ($method->isPrivate()) {
    echo "Man kann rausfinden, was man nicht clonen kann...";
}

I wonder, how stupid PHP developers get....

06/06/07 01:04:50 changed by mikey

This is just plain stupid.

class CloningHelper
{
    public static clone($object)
    {
        $clazzClazz = new ReflectionClass($object);
        if (clazzClazz->hasMethod('__clone') == true) {
            $method = $clazzClazz->getMethod('__clone');
            if ($method->isPrivate() == true || $method->isProtected() == true) {
                throw new CloneException('Cloning of ' . $clazzClazz->getName() . ' is not allowed.');
            }
        }

        $clone = clone $object;
        return $clone;
    }
}

But it does not help if the method is defined as public and throws an exception (could be catchable) or even triggers an error (not catchable) inside the method.

06/06/07 08:40:08 changed by schst

My preferred solution still is a stubCloneable interface...

I guess, this will result in a blog entry :)

06/10/07 11:02:57 changed by mikey

  • status changed from new to closed.
  • resolution set to fixed.

Fixed with changeset 720.

However there is still a case where we clone an internal PHP class: in net.stubbles.util.datespan.stubDateSpanCustom and in net.stubbles.util.datespan.stubDateSpanWeek instances of DateTime are cloned.

07/02/07 10:40:45 changed by

  • milestone deleted.

Milestone 0.2.0 deleted