php - Use method of parent class when extending -
php - Use method of parent class when extending -
probably silly question.. how correctly utilize methods of class test in class testb without overriding them?
<?php class test { private $name; public function __construct($name) { $this->name = $name; } public function getname() { homecoming $this->name; } } <?php class testb extends test { public function __construct() { parent::__construct($name); } } <?php include('test.php'); include('testb.php'); $a = new test('john'); $b = new testb('batman'); echo $b->getname();
you need give testb
constructor $name
parameter if want able initialize argument. modified testb
class constructor takes argument. way have it, should not able initialize testb
class. utilize code follows:
<?php class test { private $name; public function __construct($name) { $this->name = $name; } public function getname() { homecoming $this->name; } } class testb extends test { // added $name parameter constructor // before blank. public function __construct($name) { parent::__construct($name); } } $a = new test('john'); $b = new testb('batman'); echo $a->getname(); echo $b->getname(); ?>
perhaps not have error reporting enabled? in event, can verify results here: http://ideone.com/mhp2ox
php
Comments
Post a Comment