If you would have simply asked: how can I easily create a variable of class Y
that has all the common contents of class A
of a variable of class X
? Then I think you would have gotten a quicker answer. However you asked a question about casting.
Casting is a way of 'tricking' the compiler (or in this case the interpreter) to interpret a variable of one type as a variable of another type. If you know what you are doing this can come in handy sometimes. Most of the time however, it is a bad idea.
Casting to objects is not possible in PHP, but if it were and you could write:
class A {
public $foo = 1;
}
class X extends A {
public $bar = "2";
}
class Y extends A {
public $bom = 3;
}
$x = new X();
$y = (Y) x;
Would you want PHP to think variable $y
is of type Y or would you want it to actually be of type Y? The above would achieve the first, not the latter. As soon as you would access $y->bom
your program's execution would run into a problem, since no memory was allocated for $bom
nor was it initialized. In languages that do allow t his, like c++, this would likely cause a GPF.
Therefore you don't need to cast, you don't want to cast and you can't cast to begin with. Forget casting :)
What you want to do is create some method that copies the fields of another object to your object instance. Here is an example:
class A {
public $foo;
public function copy($cA) {
$this->foo = $cA->foo;
}
}
class X extends A {
public $bar;
public function copy($cX) {
if ($cX instanceof A) parent::copy($cX);
if ($cX instanceof X) {
$this->bar = $cX->bar;
}
}
}
class Y extends A {
public $bom;
public function copy($cY) {
if ($cY instanceof A) parent::copy($cY);
if ($cY instanceof Y) {
$this->bom = $cY->bom;
}
}
}
$x = new X();
$x->foo = "hello";
$x->bar = "world";
$otherX = new X();
$otherX->copy($x);
echo "$otherX->foo $otherX->bar \n"; // hello world
$y = new Y();
$y->copy($otherX);
echo "$y->foo \n"; // hello
The method of copying in this example is by assignment. This is not very flexible though, since this means everytime you add a property or rename a property in one of these classes you'd have to update the copy method to reflect the change.
Here is a more flexible way of achieving the same thing:
class A {
protected $foo;
public function copy($cA) {
foreach (get_object_vars($cA) as $key => $value) {
$this->$key = $value;
}
}
public function setFoo($foo) {
$this->foo = $foo;
}
}
class X extends A {
public $bar;
public function out() {
echo "$this->foo $this->bar \n";
}
}
class Y extends A {
public $bom;
public function out() {
echo "$this->foo $this->bom \n";
}
}
// setup x
$x = new X();
$x->setFoo("hello");
$x->bar = "world";
// setup otherX
$otherX = new X();
$otherX->copy($x);
$x->setFoo("hi");
$otherX->out(); // -> hello world
// setup Y
$y = new Y();
$y->copy($x);
$y->bom = "php";
$y->out(); // -> hi php
Though this method is a lot more flexible it is also somewhat crude.
echo $y->bar; // has become "world"
This happens because get_object_vars
doesn't look at what variables each class type can have. You could work around this to some extent using ReflectionClass, but I am guessing by this time you've gotten the answer you needed.
Hope this helps.
Good luck!