0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPでクラスを作成する

Posted at

PHPでクラスを作成

以下の通り、PHPのクラスのコードを書いていく

<?php
class Human{     

⇦クラス名を定義する。クラス名はPHPでは慣例的に大文字で記載する。

public $name;
public $gender;

⇦プロパティ(クラス内で定義された変数)を記載。

public function __construct($name,$gender){
     $this->name = $name;
     $this->gender = $gender;
}
}

コンストラクタはクラスからオブジェクトがnewによって作成される時に自動的に呼び出されるメソッド。
←メソッドはクラス内で定義された関数のこと。

$human = new Human('suzuki','female');
echo $human->name;
echo $human->gender;
?>

$humanをsuzuki,femaleの名前でオブジェクトを作る(インスタンス化)
echoで名前と性別を出力。

出力結果

xxxxxx@xxxxxx test-php % php class.php
suzukifemale
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?