LoginSignup
0
0

More than 3 years have passed since last update.

PHP [コンストラクタ クラスの継承]

Posted at

コンストラクタ

スクリーンショット 2020-09-29 22.05.10.png

__construct($text, $likes)
__construct($text, $likes)
newをした時にこちらの$textと$likesは下記から代入されています。
$posts[0] = new Post('hello', 0);
$posts[1] = new Post('hello again', 0);

public private

publicは、クラスの中でも外でも使用する事ができる。
privateは、クラス内でしか使用できなくする。
こういうpublic privateはアクセス修飾子と呼ばれる。

declare

declareとは、declare(strict_types=1);と最初に記述する事で、厳密に型をチェックしてくれます。

クラスの継承

スクリーンショット 2020-09-30 12.44.42.png
上記の画像の様に、
class 新しいクラス名 extends 親クラス名
とする事で、親クラスの中を引き継げる。
ちなみに、
class Post 親クラス 又は superクラス
class SponsoredPost extends Post 子クラス 又は subクラスと呼びます。 

override

親クラスと同じメソッド定義する事をoverrideという。

protected

overrideする時にプロパティのアクセス修飾子がprivateになっているとエラーが出てしまう。
そこで、privateをprotectedとする事で、そのクラスと継承したクラスしか使えなくなる。
また、overrideをうっかりして欲しくない時に、overrideして欲しくない方のアクセス修飾子の前に、finalをつけます。

0
0
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
0
0