PHPのお勉強



クラス@PHP

クラスは、変数、メソッドを持ったオブジェクトを定義した物です。
また、コンストラクタを持ちます。
コンストラクタは、オブジェクトが生成された時に、実行されます。
初期化したい処理を書いたりします。


<?php

    //クラス
class Common {
    
        //変数
        var $str;
        
        //コンストラクタ
        function Common(){
            $this->str = "Hellow";
        }
        
        //printメソッド
        function printStr(){
            print $this->str;
        }
    }

    //Commonオブジェクト作成
$commonObj = new Common();

    //文字出力
$commonObj->printStr();
    
    //Coomonオブジェクトの変数に値を格納
    $commonObj->str = ",World";
    
    //文字出力
$commonObj->printStr();
    
?>




Copyright (C) PHPのお勉強. All Rights Reserved.