탈

저번달에는 php 의 class 에 미쳐서 찾았던 TIP


<?php
Class Delegate
{
    private $arrInstances = array();
    
    protected function addObject( $oElement )
    {
        // add one element on the end of the stack  //
        $this->arrInstances[] = $oElement;
    }    
    
    public function __call( $strMethod, $arrParams )
    {
        // for each element in instance //
        foreach( $this->arrInstances as $oElement )
        {
            // get the class of the element //
            $strClass = get_class( $oElement );
            // get all methods of the class  //
            $arrMethods = get_class_methods( $strClass );
            // case the method exists into this class  //
            if( in_array( $strMethod , $arrMethods ) ) 
            {
                // prepare caller //
                $arrCaller = Array( $strClass , $strMethod );
                // return the result of the method into the object  //
                return call_user_func_array( $arrCaller, $arrParams );
            }
        }
        // any object has the method //
        // throw a exception //
        throw new Exception( " Method " . $strMethod . " not exist in this class " . get_class($this ) . "." );
    }
}

class Log
{
    public function sayHi()
    {
        print "hi!" . "<br/>\n";
    }
    
    public function sayMyName()
    {
        print "log" . "<br/>\n";
    }
}

class Other
{
    public function sayHello()
    {
        print "hello there!" . "<br/>\n";
    }
    
    public function sayMyName()
    {
        print "other" . "<br/>\n";
    }
}

class Example extends Delegate
{
    public function __construct()
    {
        $this->addObject( new Log() );
        $this->addObject( new Other() );
    }
}

$oExample = new Example();
$oExample->sayHi();
$oExample->sayHello();
$oExample->sayMyName();

/* 
    hi!<br/>
    hello there!<br/>
    log<br/>
*/
?>

|

댓글 6개

이거 어디라 사용하실려구요? 그냥 매직함수 __call 사용한 것 뿐인데...
$this->addObject( new Log() );
$this->addObject( new Other() );

이부분.......상속말고 다른 Class 의 Method 을 자신의 Method 처럼 사용하는 부분입니다.
급할땐 용이하죠...
아~ ^^ 그건 그렇죠 ... new 새로 메모리에 공간을 개설하면 메모리 작으면 좀 버벅 거릴겁니다.
저 코드는 메모리에 차지하는 용량을 무시해도 될 정도의 코드예요.....ㅠ_ㅠ
워프는 콜리앗 ... class 로 만든 저 샘플은 개미 발에 미세먼지

혹 new 로 만드는게 버벅일꺼라 생각하시는거라면.......
Framework 쪽 한번 보시는것을 추천 드립니다.
Framework 쪽으로 하고는 있습니다. 사이트는 가벼워야하니까 ^^
제가 만들어 쓰고 있는 EventDispatcher 라는 클래스와 비슷하네요.
예를 들어 게시판에 로그인이 필요한 부분에는 Dispatcher 만 해주고 관련 처리는 외부 함수를 addEventHandler 메소드로 연결해 주기만 하면 되죠.
이런걸 Observer 패턴이라고 하등가... 머 그럴겁니다.
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
16-06-03 조회 3,021
15-07-30 조회 3,414
15-06-08 조회 5,552
15-05-27 조회 4,411
15-01-08 조회 6,709
14-12-30 조회 5,442
14-10-10 조회 5,432
14-08-13 조회 6,350
14-08-11 조회 5,205
14-08-06 조회 6,538
14-03-19 조회 7,434
13-04-26 조회 9,587
12-12-15 조회 5,112
12-12-15 조회 7,347
12-12-11 조회 5,488
12-11-21 조회 6,019
12-10-11 조회 5,099
12-10-03 조회 4,790
12-09-20 조회 6,087
12-09-02 조회 4,269
12-08-29 조회 5,274
12-08-29 조회 4,159
12-08-24 조회 4,258
12-08-18 조회 4,477
12-08-17 조회 4,523
12-08-16 조회 3,775
12-08-06 조회 5,553
12-08-05 조회 4,841
12-08-03 조회 4,556
12-08-03 조회 4,703
12-07-29 조회 3,557
12-07-24 조회 4,146
12-07-22 조회 3,614
12-07-20 조회 4,372
12-07-19 조회 4,715
12-07-19 조회 3,946
12-07-19 조회 3,891
12-07-19 조회 5,561
12-07-19 조회 3,503
12-07-18 조회 5,185
12-07-15 조회 4,168
12-07-14 조회 3,838
12-07-12 조회 4,511
12-07-11 조회 3,786
12-07-11 조회 3,507
12-07-11 조회 3,480
12-07-10 조회 4,669
12-07-09 조회 3,418
12-07-09 조회 4,232
12-07-09 조회 4,653
12-07-08 조회 4,571
12-07-07 조회 5,493
12-07-06 조회 4,746
12-07-06 조회 4,013
12-07-06 조회 3,900
12-07-06 조회 3,838
12-07-02 조회 5,514
12-07-02 조회 4,212
12-06-29 조회 3,734
12-06-28 조회 4,360
12-06-26 조회 3,858
12-06-26 조회 4,973
12-06-21 조회 3,950
12-06-20 조회 4,654
12-06-20 조회 4,289
12-06-15 조회 4,642
12-06-15 조회 4,378
12-06-13 조회 7,395
12-06-05 조회 7,365
12-06-04 조회 3,944
12-06-03 조회 3,792
12-05-31 조회 3,791
12-05-28 조회 6,633
12-05-27 조회 3,871
12-05-25 조회 4,003
12-05-23 조회 4,411
12-05-21 조회 5,123
12-05-17 조회 4,060
12-05-17 조회 3,975
12-05-09 조회 4,053
12-05-07 조회 4,308
12-05-02 조회 4,497
12-05-01 조회 4,670
12-04-30 조회 3,419
12-04-30 조회 4,365
12-04-25 조회 4,452
12-04-25 조회 4,878
12-04-24 조회 3,842
12-04-24 조회 4,611
12-04-23 조회 3,609
12-04-20 조회 3,264
12-04-17 조회 5,449
12-04-16 조회 4,384
12-04-14 조회 3,668
12-04-14 조회 3,695
12-04-14 조회 3,743
12-04-13 조회 3,372
12-04-13 조회 4,560
12-04-13 조회 6,196
12-04-12 조회 3,367