3

私はC#-/。NET-guyなので、明示的なインターフェイスの実装に慣れています-次のように:

public interface IBar
{
    bool Bacon();
}

public class Foo : IBar
{
    bool IBar.Bacon() {}
}

質問:
これはPHPで行うことができますか?

編集:
明確にするために、これは暗黙的です(私が望むもの、および上記の例にあるものは明示的です):

public class Foo : IBar
{
    bool Bacon() {}
}
4

2 に答える 2

6

PHPはインターフェースをサポートしているので、可能です:http: //php.net/manual/en/language.oop5.interfaces.php

PHPは、暗黙的な実装と明示的な実装を区別しません。

于 2012-04-11T09:15:42.667 に答える
-1

以下のコードを考えると、私はコードに伴うこのタスクに非常に固執しています:

ReptileEggが孵化すると、卵を産んだのと同じ種の新しい爬虫類が作成されます。

How can a new reptile be created inside of ->hatch when Reptile is an interface? This was on a homework assignment and I wasn't able to figure it out in time. It will drive me nuts until I go back and figure it out.

<?php
interface Reptile
{
    public function layEgg() : ReptileEgg;
}

class FireDragon implements Reptile
{
    public function layEgg() : ReptileEgg {
        
    }
}

class ReptileEgg
{
    public function __construct(string $reptileType)
    {
        
    }
    
    public function hatch() : ? Reptile
    {
        return null;
    }
}
于 2021-06-01T21:56:23.460 に答える