クラスの継承 |
|
class User {
public $name;
public $email;
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function sayHi() {
echo "hi! my name is ".$this->name;
}
}
class SuperUser extends User{ //[extens User]--> Userのメンバー変数やメソッドを引き継ぐことができる。
public function superSayHi() {
echo "HHHiii! my email is ".$this->email;
}
}
|
|
|
|
$bob = new SuperUser("bob", "dummy@dummybob.com");
$bob->superSayHi();
|
|
|
( 実行結果 )
HHHiii! my email is dummy@dummybob.com |
|
|
protected |
|
public $name;
protected $email; //[protected]-- このクラス内でも、継承したサブクラス内でも使える
|
|
|
|
class User {
public $name;
protected $email; //[protected]-- このクラス内でも、継承したサブクラス内でも使える
public function __construct($name, $email) {
$this->name = $name;
$this->email = $email;
}
public function sayHi() {
echo "hi! my name is [ ".$this->name." ] and email is [ ".$this->email." ]";
}
}
class SuperUser extends User { //[extens User]--> Userのメンバー変数やメソッドを引き継ぐことができる。
public function superSayHi() {
echo "HHHiii! my email is [ ".$this->email." ]";
}
}
$bob = new SuperUser("bob", "bob@dummy.com");
$bob->sayHi();
$bob->superSayHi();
|
|
|
|
|
|