Hey all,
I've been having a hard time coming up a solution with this one. I'm hoping you all can help me out.
Best described with an example:
class Parent {
public $nationality;
function __construct($nationality)
{
$this->nationality = $nationality
}
}
class Child extends Parent {
function __construct() {
echo $this->nationality; // hispanic
}
}
// Usage:
$parent = new Parent('hispanic');
$child = new Child();
I want the child to inherit properties and methods from a parent that is already initialized.
EDIT: Thanks all for the responses - let me give you some background. I'm trying to make a templating system. I have two classes - say Tag.php, and Form.php.
I'd like it to look like this:
class Tag {
public $class_location;
public $other_tag_specific_info;
public $args;
function __construct($args)
{
$this->args = $args;
}
public function wrap($wrapper) {
...
}
// More public methods Form can use.
}
class Form extends Tag {
function __construct() {
print_r($this->args()) // 0 => 'wahoo', 1 => 'ok'
echo $this->class_location; // "/library/form/form.php"
$this->wrap('form');
}
function __tostring() {
return '<input type = "text" />';
}
}
// Usage:
$tag = new Tag(array('wahoo', 'ok'));
$tag->class_location = "/library/form/form.php";
$tag->other_tag_specific_info = "...";
$form = new Form();
The reason I don't like the composite pattern is that it doesn't make sense to me why I would be passing in an instance of Tag to the constructor of one of its subclass, afterall - Form is a type of Tag right?
Thanks! Matt Mueller