WHERE'S THE BEEF?
Looks like type-casting user-defined objects is a real pain, and ya gotta be nuttin' less than a brain jus ta cypher-it. But since PHP supports OOP, you can add the capabilities right now. Start with any simple class.
<?php
class Point {
protected $x, $y;
public function __construct($xVal = 0, $yVal = 0) {
$this->x = $xVal;
$this->y = $yVal;
}
public function getX() { return $this->x; }
public function getY() { return $this->y; }
}
$p = new Point(25, 35);
echo $p->getX(); echo $p->getY(); ?>
Ok, now we need extra powers. PHP gives us several options:
A. We can tag on extra properties on-the-fly using everyday PHP syntax...
$p->z = 45; // here, $p is still an object of type [Point] but gains no capability, and it's on a per-instance basis, blah.
B. We can try type-casting it to a different type to access more functions...
$p = (SuperDuperPoint) $p; // if this is even allowed, I doubt it. But even if PHP lets this slide, the small amount of data Point holds would probably not be enough for the extra functions to work anyway. And we still need the class def + all extra data. We should have just instantiated a [SuperDuperPoint] object to begin with... and just like above, this only works on a per-instance basis.
C. Do it the right way using OOP - and just extend the Point class already.
<?php
class Point3D extends Point {
protected $z; public function __construct($xVal = 0, $yVal = 0, $zVal = 0) {
parent::__construct($xVal, $yVal);
$this->z = $zVal;
}
public function getZ() { return $this->z; } }
$p3d = new Point3D(25, 35, 45); echo $p3d->getX(); echo $p3d->getY(); echo $p3d->getZ(); ?>
Once the new class definition is written, you can make as many Point3D objects as you want. Each of them will have more data and functions already built-in. This is much better than trying to beef-up any "single lesser object" on-the-fly, and it's way easier to do.