目次 |
---|
・クラス ・クラスをインスタンス化する ・クラスのコンストラクタ ・クラスの継承 ・クラスのアクセス権 |
<?php class sample{ var $msg = "サンプルです"; function msg(){ echo $this->msg; } } ?>
<?php class sample{ var $msg = "サンプルです"; function msg(){ echo $this->msg; } } $a = new sample(); echo $a->msg; $a->msg(); ?>
サンプルですサンプルです
<?php class sample01{ var $msg = "サンプルです"; function msg(){ echo $this->msg; } } // クラス名を変数に入れて、new演算子を使用する $class_name = "sample01"; $a = new $class_name(); echo $a->msg; $a->msg(); ?>
サンプルですサンプルです
<?php class sample1{ var $msg = "サンプルです"; // コンストラクタ function sample1(){ echo $this->msg; } } $b = new sample1(); ?>
<?php class sample2{ var $msg = "サンプルです"; // コンストラクタ function __construct(){ echo $this->msg; } } $b = new sample1(); ?>
<?php class sample3{ var $msg = "親でーす"; var $msg1 = "親でーす"; // コンストラクタ function __construct(){ echo $this->msg; echo $this->msg1; } } class sample4 extends sample3{ var $msg = "子でーす"; // コンストラクタ function __construct(){ echo $this->msg; echo parent::__construct();//親クラスのコンストラクタ呼び出し } } $b = new sample4(); ?>
子でーす子でーす親でーす
<?php class sample5{ var $msg = "親でーす"; var $msg1 = "親でーす"; // コンストラクタ function __construct(){ echo $this->msg; echo $this->msg1; } } class sample6 extends sample5{ var $msg = "子でーす"; } $b = new sample6(); ?>
子でーす親でーす
<?php class sample7{ private $msg = "親でーす"; protected $msg1 = "親でーす"; // コンストラクタ function __construct(){ echo $this->msg; echo $this->msg1; } } class sample8 extends sample7{ var $msg = "子でーす"; var $msg1 = "子でーす"; } $b = new sample8(); ?>
親でーす子でーす