平成30/ 2018-10-22 00:46
PHP  CLASS                   /2016.12.19


#######################
# 継承

class AAA{
    public static function aaaa(){
        echo 'aaa';
    }
}
class BBB extends AAA{
    public static function aaaa(){
        echo 'bbb';
    }
}

BBB::aaaa();
/*
bbb が表示される。
*/



#######################
# PHP.ver.5.4 から、関数から要素指定で、値取得できる。

class This{
    public static function addr(){
        return array(
            'pref'=>'埼玉',
            'city'=>'さいたま市',
        );
    }
}

echo This::addr()['city'];


################################
# class も条件分岐で切り替えすることができる。

$ php -r 'if(1){class a{function aaa(){echo 1;}}}else{class a{function aaa(){echo 2;}}}a::aaa();'
1

$ php -r 'if(0){class a{function aaa(){echo 1;}}}else{class a{function aaa(){echo 2;}}}a::aaa();'
2




---------------------------------------------------------------------------
$ php -r 'class AAA{ const DATE="Ymd"; } echo AAA::DATE;'
Ymd

以下のように関数の値は代入できない。
$ php -r 'class AAA{ const DATE=date("Ymd"); }'
PHP Parse error:  syntax error, unexpected '(', expecting ',' or ';' in Command line code on line 1






---------------------------------------------------------------------------
>> プロパティ
class This{
    static $aaa1 = "";
    static public $aaa2 = array();
    const title = 'aaa3';
    static protected $aaa5 = "aaa5txt";
    public $aaa6 = "aaa6txt";
    static private $aaa7 = "aaa7txt";
    private $aaa8 = array();
    static private $aaa9 = array();

    function __construct(){
        $this->aaa8 = array("a"=>1,"b"=>2);
        This::$aaa9 = array("c"=>3,"d"=>3);
    }

    public static function aaa4(){
        echo 'konnnitiha';
    }
    function aaa8(){
        return $this->aaa8;
    }
    function aaa9(){
        return This::$aaa9;
    }
}
new This();

foreach(get_class_vars("This") as $key => $val){
    echo $key. "\n";
    if(
    is_array($val)
    ){
        print_r($val);
        echo "\n";
    } else{
        echo $val. "\n";
    }
}
echo This::title;
echo "\n";

echo This::aaa4();
echo "\n";

//echo This::$aaa5;
//echo "\n";
//PHP Fatal error:  Cannot access protected property This::$aaa5

//echo This::$aaa6;
//echo "\n";
//PHP Fatal error:  Access to undeclared static property: This::$aaa6

//echo This::$aaa7;
//echo "\n";
//PHP Fatal error:  Cannot access private property This::$aaa7

//print_r(This::aaa8());
//echo "\n";
//PHP Fatal error:  Using $this when not in object context

print_r(This::aaa9());
echo "\n";
/*
Array
(
    [c] => 3
    [d] => 3
)
*/



>> 仮クラスでエラー回避
class B { function user($str) { } }
$auth = new B(); // エラー出るので仮のクラスを設定
require_once dirname($_SERVER["DOCUMENT_ROOT"]). "/views/elements/admin/header.html";


>> Thanks.
クラスの基礎 プロパティ
http://www.php.net/manual/ja/language.oop5.properties.php