PHP復習

環境

mfham@mac ~/w/php_study> php -v
PHP 7.4.5 (cli) (built: Apr 23 2020 23:35:46) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

インスタンス化する・しないでの挙動

<?php

class Human
{
    const COUNTRY = ‘jp’;

    public static $hobby = 'sleeping';
    public        $job   = 'developer';
    private       $name  = 'mfham';

    public function public_hello()
    {
        echo 'public_hello';
    }

    private function private_hello()
    {
        echo 'private_hello';
    }

    public static function public_static_hobby()
    {
        return self::$hobby;
    }

    public static function public_job()
    {
        return self::$job;
    }
}

// インスタンス化する
$human = new Human();

echo $human->job;  // => ‘developer'
echo $human->name; // Fatal error: Uncaught Error: Cannot access private property Human::$name

$human->public_hello();  // => ‘public_hello'
$human->private_hello(); // PHP Fatal error:  Uncaught Error: Call to private method Human::private_hello() from context ‘'

// インスタンス化しない
echo Human::COUNTRY; // ‘jp'
echo Human::$hobby;  // ’sleeping'
echo Human::$job;    // Fatal error: Uncaught Error: Access to undeclared static property: Human::$job 

echo Human::public_static_hobby(); // ’sleeping’
echo Human::public_job();          // PHP Fatal error:  Uncaught Error: Access to undeclared static property: Human::$job

オーバーライド

<?php

class ParentClass
{
    public function func1($v)
    {
        echo ‘hello parent';
    }
}

class ChildClass extends ParentClass
{
    public function func1($vv)
    {
        echo $vv;
    }
}

// オーバーライド
$child = new ChildClass();
$child->func1('foo’); // ‘foo’;
<?php

class ParentClass
{
    public function func1($v)
    {
        echo ‘hello parent';
    }
}

class ChildClass extends ParentClass
{
    public function func1()
    {
        echo ‘hello child';
    }
}

// 引数の違うオーバーライドはWARNING
$child = new ChildClass(); // PHP Warning:  Declaration of ChildClass::func1() should be compatible with ParentClass::func1($v)
$child->func1(); // ‘hello child’

finalキーワード

<?php

class ParentClass
{
    public final function func1()
    {
        echo 'hello parent';
    }
}

class ChildClass extends ParentClass
{
    public function func1()
    {
        echo 'hello child';
    }
}

$child = new ChildClass(); // PHP Fatal error:  Cannot override final method ParentClass::func1()

abstract

インスタンス化できない。

<?php

abstract class ParentAbstract
{
    abstract public function func1();

    public function func2()
    {
        echo 'parent func2';
    }
}

$parent = new ParentAbstract(); // PHP Fatal error:  Uncaught Error: Cannot instantiate abstract class ParentAbstract

abstractキーワードをつけたメソッドは実装する必要がある。

<?php

abstract class ParentAbstract
{
    abstract public function func1();

    public function func2()
    {
        echo 'parent func2';
    }
}

class ChildClass extends ParentAbstract
{
    public function func2()
    {
        echo 'child func2';
    }
}

$child = new ChildClass(); // PHP Fatal error:  Class Child contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ParentAbstract::func1)

interface

インスタンス化できない。

<?php

interface ParentInterface
{
    public function func1();
}

$parent = new ParentInterface(); // PHP Fatal error:  Uncaught Error: Cannot instantiate interface ParentInterface

メソッドを実装しなければいけない。

<?php

interface ParentInterface
{
    public function func1();
}

class ChildClass implements ParentInterface
{
}

$child = new ChildClass(); // PHP Fatal error:  Class Child contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ParentInterface::func1)

遅延静的束縛

<?php

class ParentClass
{
    public function hello1()
    {
        self::hi1();
    }

    public function hello2()
    {
        static::hi2();
    }

    public static function hi1()
    {
        echo 'parent hi1';
    }

    public static function hi2()
    {
        echo 'parent hi2';
    }
}

class ChildClass extends ParentClass
{
    public static function hi1()
    {
        echo 'child hi1';
    }

    public static function hi2()
    {
        echo 'child hi2';
    }
}

$child = new ChildClass();

$child->hello1(); // ‘parent h1'
$child->hello2(); // ‘child h2'