re-casting objects in PHP

    Date: 02/12/06 (PHP Development)    Keywords: php

    Hi,

    Perl lets me do this:

    package A;
    sub new {
    my ($class, @params) = @_;
    my $self = { };
    bless $self, ref($class) || $class;
    return $self;
    }

    --

    package B;
    @ISA = qw(A);

    --

    my $a = new A;
    bless $a, "B";
    # $a is now an object of class B.

    --

    The equivalent in PHP to the class heirarchy is this:

    class A {
    function A() { };
    }

    class B extends A {
    function B() { };
    }

    Now is it possible to write some code that allows "upgrading" an object of class A to be an object of class B? i.e. I want to do something equivalent to the perl "bless" function, which turns a reference into an object, however I want to do it to an object, e.g.

    $a = new A();
    $b = $a->transform_to_class_B();

    ... or similar.

    Has anyone figured out a PHP 4 or 5 way of doing this?

    Source: http://community.livejournal.com/php_dev/65642.html

« PHP and XML parsing || PEAR »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home