Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /data/e/7/e76990b9-3372-4d88-8089-fce36e16744a/webperfection.net/sub/rady-nosu/wp-content/plugins/seo-ultimate/modules/class.su-module.php on line 1195
php7 invariants and signature validation or class return type | Real Solution for Your Problem!

December 9, 2015 | In: Programovanie

php7 invariants and signature validation or class return type

Variance and Signature Validation – basically in simple layman’s term – class return type

then interesting thing I find about the example from


class A {}

interface SomeInterface
{
    public function test() : A;
}

class B implements SomeInterface
{
    public function test() : A // all good!
    {
        return null;
    }

    public function test2() : string
    {
        return 'wow';
    }
}

// all good, will not fail
$b = new B();

// will fail with TypeError: 
// Return value of B::test() must be an instance of A, null returned 
// $b->test();

$b->test2(); // all good

more about php7:

if you need to install PHP7 check out this article

php7 null coalesce operator ??

Share This:

Comments are closed.