LoginSignup
1
Organization

PHP(Laravel) constructを使ったクラスについて解説

今回やること

今回は、constructを使ったクラスについて解説をしていく。

STEP1

<?php

class test{
    private $name;
    function __construct($test2){
        $this->name = $test2;
    }
    function show(){
        echo $this->name;
    }
}

今回の場合は、testがインスタンス化されたら、__constructが発動して、$this(クラスthisのこと)の中にあるnameに、引数の値を入れるという処理。

そして、showで、nameの値を表示するといった処理が書かれている。

STEP2

実際に表示をする作業に入る。

$test4 = new test("aaa");
$test4->show();
?>

test4で、クラスtestをインスタンスして、引数として、aaaを入れている。

そして、test4の中のshowを呼び出すといった構成になっている。

まず、aaaの値は、nameに格納され、nameを表示するという処理のshowを呼び出すことで、nameに格納されたaaaを表示するといった流れ。

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
What you can do with signing up
1