2013-07-02 4 views
0

두 개의 매우 기본적인 클래스 인 Chapter과 Book이 있다고 가정 해 보겠습니다.OOP PHP - 클래스 초보 운동

PHP 코드 :

/** 
* Class Chapter 
*/ 
class Chapter 
{ 
    private $title; 

    public function __construct($title) 
    { 
     $this->title = $title; 
    } 

    public function getTitle() 
    { 
     return $this->title; 
    } 

    public function loadChapterTitle() 
    { 
     $title = $this->getTitle(); 
     echo $title; 

     return $title; 
    } 
} 

/** 
* Class Book 
*/ 
class Book 
{ 
    // 
} 

사용 예 : OOP에서

$myTitleArray = array('first','second','third'); 
myBook = new Book($myTitleArray); 

$myBook->loadBookIndex(); // echo: first, second, third 

, 그 loadBookIndex() 메소드와 함께 예약 클래스를 정의하는 가장 우아한 방법은?

편집 : 그냥 OO 교훈 목적을 위해 ... loadBookIndex() 챕터를 사용하기로되어 있습니다.

+1

Whats은 (는) BookIndex입니다. 코드 게시 없음 – 75inchpianist

+0

'BookIndex'를 클래스로 선언하지 않았습니다 ... 호출 할 수 없습니다 ... 오류가 발생합니다 – ErickBest

+0

코드에서 loadBookIndex()는 BookIndex 클래스의 멤버입니다 . 나는 우리에게 조금 더 많은 정보가 필요하다고 생각한다. –

답변

2

책은 본질적으로 챕터 목록입니다. 각 장에는 제목과 텍스트가 있습니다. 책 객체가 색인 작성의 책임을 처리하도록하는 것은 어떻습니까? 이 같은

<?php 
class Chapter { 

    public $title; 
    public $text; 

    public function __construct($title, $text) { 
     $this->title = $title; 
     $this->text = $text;  
    } 
} 

class Book { 
    private $chapters; 

    public function __construct() { 
     $this->chapters = array(); 
    } 

    public function addChapter(Chapter $chapter) { 
     $this->chapters[] = $chapter; 
    } 

    public function getIndex() { 
     $index = array(); 

     foreach($this->chapters as $chapter) { 
      $index[] = $chapter->title; 
     } 

     return $index; 
    }  
} 

// Usage 
$book = new Book("foo"); 
$book->addChapter(new Chapter("Foreword", "Blabla")); 
$book->addChapter(new Chapter("Introduction", "Blabla")); 
$book->addChapter(new Chapter("Conclusion", "Blabla")); 

$index = $book->getIndex(); // array(foreword, introduction, conclusion) 
+0

감사합니다. 더 나은 getIndex() 아마 : $ index [] = $ chapter-> loadChapterTitle(); ? –

+0

@Guido 로스 확신. loadChapterTitle (나는'getTitle'을 추천한다)의 이름을 바꾸고 그 메소드에서 echo를 제거하면된다. – alexn

+0

addChapter()와 foreach를 어떻게 비교합니까? $ this-> chapters [] = new Chapter ($ title); 다른 대답에서? –

0

사용 현황을 변경할 수 없습니다 가정/주어진 것을, 내가 할 줄 뭔가 : 그러나

class Book { 
    private $chapters = array(); // array to contain chapters 

    public function __construct(array $chapterTitles) { 
     // Create a new instance of class "Chapter" for each chapter and store 
     // it in the $chapters array 
     foreach ($chapterTitles as $title) { 
      $this->chapters[] = new Chapter($title); 
     } 
    } 

    public function loadBookIndex() { 
     // Iterate over all chapters and load chapter information 
     $index = array(); 
     foreach ($this->chapters as $chapter) { 
      $index[] = $chapter->loadChapterTitle(); 
     } 
     return $index; 
    } 
} 

, 그 "로드"방법 중 특히 이름은 이러한 방법으로 잘못된 것 실제로 아무것도로드하지 마십시오.

+0

로드가 아닌 인쇄를 편집해야 할 수도 있습니다. –

+0

@GuidoRossi getIndex/buildIndex/compileIndex 또는 비슷한 이름으로 바꾸는 것이 좋습니다. 책을 대표하는 클래스 (실제로 어떻게 알았는지)가 실제로 인쇄하거나 반향하는 것은 이해가되지 않습니다. 이 파일을 파일에 쓰려면 어떻게해야할까요? 발신자가 색인을 사용하여 수행 할 작업을 결정하게하십시오. – alexn