Reputation: 612
All,
If I have a class like so:
class MyClass {
var $height;
var $width;
function setDimensions($height,$width) {
$this->height = $height;
$this->width = $width;
}
}
And another class like so...
class AnotherClass extends MyClass {
var $color;
var $speed;
function setFeatures($color,$speed) {
$this->color = $color;
$this->speed = $speed;
}
function showAll() {
echo "Color: ".$this->color."<br />";
echo "Speed: ".$this->speed."<br />";
echo "Height: ".$this->height."<br />";
echo "Width: ".$this->width."<br />";
// echo "Height: ".parent::height."<br />";
}
}
Then I do this:
$firstClass = new MyClass();
$firstClass->setDimensions('200cm', '120cm');
$secondClass = new AnotherClass();
$secondClass->setFeatures('red','100mph');
$secondClass->showAll();
It does not print the properties defines in the $firstClass. This is understandable as they are two separate instances/objects. How can I pass the properties from one object to another? Would I need to do something like $secondClass = new AnotherClass($firstClass)
and pass it that way?
Thank you in advance, any help is appreciated.
Upvotes: 0
Views: 32
Reputation: 5512
Because they are different instances.
In this example there is one instance which is being both AnotherClass and MyClass at the same time
$firstClass = new AnotherClass();
$firstClass->setDimensions('200cm', '120cm');
$firstClass->setFeatures('red','100mph');
$firstClass->showAll();
None of this classes is abstract.
In your example, the following statements are true:
Also, please do not use var
to define classes properties.
Use either private
, public
or protected
:
class MyClass {
public $height;
public $width;
Upvotes: 2