생활코딩

Coding Everybody

코스 전체목록

닫기

Interface

수업소개

인터페이스는 서로 다른 시스템이 결합되는 접점을 의미합니다. 이 때 서로 시스템이 잘 결합하기 위해서는 상호간에 엄격한 약속이 필요합니다. 프로그래밍에서의 인터페이스도 마찬가지입니다. 이 수업에서는 프로그래밍에서는 어떻게 인터페이스를 구현하는가를 알아보겠습니다. 

수업

소개

<?php
interface ContractInterface
{
    public function promiseMethod(array $param):int;
}
interface ContractInterface2
{
    public function promiseMethod2(array $param):int;
}

class ConcreateClass implements ContractInterface, ContractInterface2
{
    public function promiseMethod(array $param):int
    {
        return 1;
    }
    public function promiseMethod2(array $param):int
    {
        return 1;
    }
}
$obj = new ConcreateClass();
$obj->promiseMethod([1,2]);

사례

<?php
interface ContractInterface
{
    public function compare(string $str1, string $str2):bool;
}
class Concreate implements ContractInterface
{
    public function compare(string $str1, string $str2):bool
    {
        if($str1 === $str2)
            return true;
        else
            return false;
    }
}
class Dummy implements ContractInterface
{
    public function compare(string $str1, string $str2):bool
    {
        return true;
    }
}
$obj = new Concreate();
if ($obj->compare('test1', 'test2')) {
    echo '<h1>same</h1>';
} else {
    echo '<h1>different</h1>';
}

사례

모노로그에서 인터페이스를 사용하는 사례를 살펴봅니다. monolog에 대한 자세한 설명은 아래 수업을 참고하세요. https://opentutorials.org/module/6/15756

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\NativeMailerHandler;

$log = new Logger('name');
$log->pushHandler(new StreamHandler(__DIR__ . '/app.log', Logger::ERROR));
$log->pushHandler(new NativeMailerHandler(
    'egoing@gmail.com',
    'Emergence!!!!',
    'out@system.com',
    Logger::EMERGENCY));

$log->warning('EGO');
$log->error('ING');
$log->emergency('emergency');

댓글

댓글 본문
버전 관리
egoing
현재 버전
선택 버전
graphittie 자세히 보기