1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/* ------------------------------------------------------------------------------
 *  クラス・オブジェクト:-- ◆クラスの理解のためのプログラミング記述◆ --の例
 *  2015.10.15 作成 yoshi of CXMedia Inc.
 * ------------------------------------------------------------------------------
 */
header("Content-type:text/plain; charset=UTF-8");
/* -------------------
 *   クラスの構成
 * ------------------- */
// ---スーパークラス--- //
class MyClass {
    
// プロパティ
    
public $name;
    
    
// コンストラクタ
    
function __construct($str){
        
$this->name $str;
    }
    
    
// メソッド
    
public function show(){
        echo 
$this->name,"\n";
    }
    
    
// デストラクタ(省略可能)
    
function __destruct(){
    }
}
// ---サブクラス:"extends"を付加したクラス名でクラスの継承--- //
class ExtClass extends MyClass {
    
// プロパティの追加
    
public $role;
    
    
// メソッド:show()メソッドをオーバーライド
    
public function show(){
        echo 
$this->role,"\n";
    }
    
// メソッドの追加
    
public function save(){
        
$this ->role $this ->name;
    }
}

/* --------------
 *  クラスの利用
 * -------------- */
// --スーパークラスのオブジェクトの記述--//
$obj = new MyClass'sample' );

// プロパティ参照と設定の記述
echo $obj ->name,"\n";
$obj->name 'product';
 
// メソッド実行の記述
$obj->show();

// --サブクラスのオブジェクトの記述--//
$objx = new ExtClass'chief' );

// メソッド実行の記述
$objx->save();
$objx->show();
?>