I am looking for a way to call the constructor of the parent class (?) Automatically-magically from the child class:
(Note: this is just an example, so input errors may occur)
Class myParent() { protected $html; function __construct( $args ) { $this->html = $this->set_html( $args ); } protected function set_html( $args ) { if ( $args['foo'] === 'bar' ) $args['foo'] = 'foobar'; return $args; } } Class myChild extends myParent { public function do_stuff( $args ) { return $this->html; } } Class myInit { public function __construct( $args ) { $this->get_stuff( $args ); } public function get_stuff( $args ) { $my_child = new myChild(); print_r( $my_child->do_stuff( $args ) ); } } $args = array( 'foo' => 'bar, 'what' => 'ever' ); new myInit( $args ); // Should Output: /* Array( 'foo' => 'foobar', 'what' => 'ever' ) */
What I want to avoid is to call (inside the myChild class) __construct( $args ) { parent::__construct( $args ); } __construct( $args ) { parent::__construct( $args ); } .
Question: Is this possible? If yes: How?
Thanks!
source share