Toshusai blog

知識の保管庫

PHPの備忘録

PHPの備忘録

<?php
//コメントアウト
/*
複数行コメントアウト
*/
define("NAME", "VALUE");

function fizzBuzz(){
    for($i = 1; $i <= 100; $i++){
        if ($i % 3 == 0) {
            echo 'Fizz';
        }
        if ($i % 5 == 0) {
            echo 'Buzz';
        }
        if (!($i % 3 == 0) && !($i % 5 == 0)) {
            echo $i;
        }
        echo "\n";
    }
}

abstract class Vector2{
    protected $x;
    protected $y;

    function __construct($x, $y){
        $this->x = $x;
        $this->y = $y;
    }

    abstract function toString();
}

class Vector3 extends Vector2{
    private $z;

    function __construct($x = 0, $y = 0, $z = 0){
        parent::__construct($x, $y);
        $this->z = $z;
    }

    function toString(){
        //文字列の中に変数をかける
        return "($this->x, $this->y, $this->z)";
    }

    function Hoge(){
        return true;
    }
}

$origin = new Vector3();
echo $origin->toString();
//(0, 0, 0)

//文字列の変数からクラスのインスタンスを作ったり、関数を呼び出すこともできる
$vector3String = "Vector3";
$toStringString = "toString";
$one = new $vector3String(1, 1, 1);
echo $one->$toStringString();
//(1, 1, 1)


?>