2
3

More than 3 years have passed since last update.

PHP Construct,Getter,Setter,オーバーライドについてまとめてみた

Last updated at Posted at 2021-02-17

はじめに

PHPの
Construct,Getter,Setter,オーバーライドについて
まとめてみた。

Construct,Getter,Setter,オーバーライド
 などが、よくわからない方へおすすめ

Construct (コンストラクタ)

Construct (コンストラクタ)
インスタンス(new)された時点で、自動で呼び出されるメソッドのこと。

※使い道
インスタンス作成時の初期化
 (※初期化はあくまでも初期値の設定、
  最初の仕様に戻すこと。
  値が0になったりり、消えたりする訳ではない)

サンプルコード
obj-sample.php
<?php 
// クラスの定義
class ParentClass {

    // コンストラクタの定義
    public function __construct () {
        echo "コンストラクタの呼び出しに成功";
    }
}

// インスタンスの作成 ※この時点でコンストラクタが呼ばれる
$parentClass = new ParentClass();

// 実行結果: コ ン ス ト ラ ク タ の 呼 び 出 し に 成 功   
 ?>

Getter (ゲッター) Setter (セッター)

・Getter
メンバ変数の値を取得するために
引数とreturnなどを使用し、
呼び出し元に返すメソッドの総称

・Setter
メンバ変数に値をセットしたり、変更したりするために
引数などを利用したメソッドの総称

※使い道
・フィールドアクセスの制御
・カプセル化
・値の加工
・値の検査

サンプルコード
obj-sample1.php
<?php 

// クラスの定義
class ParentClass {

    // プロパティの定義
    protected $number;
    protected $name;

    // コンストラクタの定義
    public function __construct ($number,$name) {
        $this->number = $number;
        $this->name = $name;
    }

    // ゲッター ($numberプロパティの値を返す)
    public function getNumber () {
        return $this->number;
    }

    // ゲッター ($nameプロパティの値を返す)
    public function getNmae () {
        return $this->name;
    }

    // セッター (型の判別)
    public function setDecision($number, $name) {
        if (!is_int($number)) {
            throw new LogicException('int型でないのでエラー');   
        }
        elseif (!is_string($name)) {
            throw new LogicException('string型でないのでエラー');   
        }
        $this->number = $number;
        $this->name = $name;
    }

    // メソッド
    public function showMethod () {
        echo $this->number.' '.$this->name.PHP_EOL;
    }
}

// インスタンスの作成 ※この時点でコンストラクタが呼ばれる
$parentClass = new ParentClass(1, 'name');

// ゲッターの実行
$parentClass->getNumber();
$parentClass->getNmae();


// メソッドの実行
$parentClass->showMethod();

// 実行結果: 1 name                                                                                                           
 ?>

オーバーライド

親クラスメソッドと同じメソッド名を子クラスで記述し、
メソッド内の処理内容を更新すること

サンプルコード
obj-sample2.php
<?php 
// 外部ファイルの呼び出し
require_once('obj-sample1.php');

// クラスの継承
class ChildClass extends ParentClass {

    // 親クラスのコンストラクタの呼び出し      
    public function __construct() {
        parent:: __construct($number, $name);
    }

 //    オーバーライドされるメソッド(親クラスに記述されているメソッド)
 //    public function showMethod () {
 //        echo $this->number.' '.$this->parentName.$this->name.PHP_EOL;
 //    }

    // メソッドをオーバーライド
    public function showMethod () {
        echo $this->number.' '.$this->childName.$this->name.PHP_EOL;
    }   
}

// 継承したクラスのインスタを作成 ※この時点で、親クラス,子クラスのコンストラクタが呼ばれる
$chlidClass = new ChildClass();


// 継承されたセッターで、値の変更
$chlidClass->setDecision(2, 'name');

// 継承されたメソッドの実行
$chlidClass->showMethod();

// 実行結果:
// 1name
// 2 name

 ?>

全体サンプルコード

obj-sample3.php
<?php 
// クラスの定義
class ParentClass {

    // プロパティの定義
    protected $number;
    protected $name;
    protected $parentName = '親クラスの';
    protected $childName = '子クラスの';

    // コンストラクタの定義
    public function __construct ($number,$name) {
        $this->number = $number;
        $this->name = $name;
    }

    // ゲッター ($numberプロパティの値を返す)
    public function getNumber () {
        return $this->number;
    }

    // ゲッター ($nameプロパティの値を返す)
    public function getName () {
        return $this->name;
    }

    // セッター (型の判別)
    public function setDecision($number, $name) {
        if (!is_int($number)) {
            throw new LogicException('string型でないのでエラー');   
        }
        elseif (!is_string($name)) {
            throw new LogicException('string型でないのでエラー');   
        }
        $this->number = $number;
        $this->name = $name;
    }

    // メソッド
    public function showMethod () {
        echo $this->number.' '.$this->parentName.$this->name.PHP_EOL;
    }
}

// インスタンスの作成 ※この時点でコンストラクタが呼ばれる
$parentClass = new ParentClass(1, 'name');

// ゲッターの実行
$parentClass->getNumber();
$parentClass->getName();

// メソッドの実行
$parentClass->showMethod();
// 実行結果:
// 1 親 ク ラ ス の name    
 ?>
obj-sample4.php
<?php 
// 外部ファイルの呼び出し
require_once('obj-sample3.php');

// クラスの継承
class ChildClass extends ParentClass {

    // 親クラスのコンストラクタの呼び出し      
    public function __construct() {
        parent:: __construct($number, $name);
    }

    // メソッドをオーバーライド
    public function showMethod () {
        echo $this->number.' '.$this->childName.$this->name.PHP_EOL;
    }   
}

// 継承したクラスのインスタを作成 ※この時点で、親クラス,子クラスのコンストラクタが呼ばれる
$chlidClass = new ChildClass();


// 継承されたセッターで、値の変更
$chlidClass->setDecision(2, 'name');

// 継承されたメソッドの実行
$chlidClass->showMethod();

// 実行結果: 
// 1 親 ク ラ ス のname                                                                                                       
// 2 子 ク ラ ス の name    
 ?>

終わりに

・Construct
インスタンス(new)した時点で、毎回呼び出されるメソッド

・Getter
メンバ変数の値を取得したいりするメソッドの総称

・Setter
メンバ変数の値をセットしたりするメソッドの総称

・オーバーライド
親クラスのメソッドを子クラスで更新すること

※余談
カプセル化するに当たって、
継承されるClassのメソッドのアクセス端子は
protectedになるが、
そもそも子クラスでメンバ変数を
更新できてること自体どうなのだろか

以上のことについて思うことがあれば
コメントの方をお願い致します。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3