Adam Baranyai
Adam Baranyai

Reputation: 3867

Can you clone an object's parent object in php?

Is it possible to clone an object's parent object only in PHP? I've tried using this code but it returns a Fatal error: __clone method called on non-object error message.

Here's my code:

class A {
    public $val1=1;
    public $val2=2;
}

class B extends A {
    public $val3=3;

    public function getParent() {
        return clone parent;
    }
}

$B=new B();
$A=$B->getParent();

The way I inted to use this, would be the following:
Let's propose I have to classes: Table and Record extends Table. The Table class holds information about a MySQL database table, the fields it has, the default values of these fields, the primary key, and most importantly, it holds three methods(insert, delete, update), but we will only talk about delete here. These three methods are stored as anonymous functions in a protected array inside the class. The delete method of the table class, tells what to do, when deleting something from the respective table.

For example, we have the following three mysql tables: parent, children, grandchildren. These tables are linked together in the following way: A record in the parent table can be linked to several records in the children table, while a record in the children table can be linked to several records in the grandchildren table.

So in this example, the Table object of the parent MySQL table would tell us, that WHEN we delete a record from this table, based on the primary key of the table, we should delete every record from the children table which is linked to this record. The delete method of the children table says something simmilar: when we delete a record from the table, we should delete every record from the grandchildren table, which was linked to the children table just now deleted.

When I will initialize objects in my code, I will initialize only Record extends Table objects. The first time, a Table object is initialized via a Record extends Table object, it should be stored in a static variable of the Table class, for future use, so when deleting something from a table, I don't have to make a new Record, I just use: Table::$tables['parent']->delete(5); which will delete the record from that table which has the primary key of 5, and will delete everything else that is needed to be deleted.

I don't know if this makes any sense(it can either be incoherensive because english is not my native tongue, or because it is rather late at my side of the Earth, and perhaps there is a much easier and cleaner solution to my problem then the one imagined by me)

Upvotes: 1

Views: 932

Answers (1)

hek2mgl
hek2mgl

Reputation: 158020

No, you can't (in PHP). The proper way in this case would be:

class B extends A {
    public $val3=3;

    public function toA() {
        $a = new A();
        // Note: If $this->val is an object value you need to
        // use $a->val = clone $this->val;
        $a->val1 = $this->val1;
        $a->val2 = $this->val2;
        return $a;
    }
}

However, a proper OOP design should not require such a method. Parts which are written to handle an A can simply handle a B like an A since B extends from A. I mean that's one of the basic ideas of inheritance.

Upvotes: 1

Related Questions