2017-12-18 5 views
-3

저는 PHP를 처음 사용하고이 코드를 작성했습니다. 여기에는 클래스와 인스턴스가 두 개 포함되어 있습니다. 이 클래스에는 모든 제목 단어가 ucwords() 함수로 대문자로 표시 될 때 인스턴스 당 표시 할 개인 title 속성에 액세스하는 setter 및 getter 메서드가 들어 있습니다. 또한 해당 컨텍스트에서 "authors"속성을 포함합니다.PHP 클래스에서 Setter-getter 기능이 실패합니다.

코드를 실행할 때 아무것도 얻지 못했지만 (아무런 의미도 갖지 못했습니다 (titleauthor도) 오류가 없으므로 내가 잘못한 것을 알지 못합니다. (개인 운동의 일환으로 수행합니다. teamtreehouse.com에서 학습 할 때).

class Recipe { 
    private $title; 
    public $author = "Me myself"; 

    public function setTitle($title) { 
     echo $this->title = ucwords($title); 
    } 
    public function getTitle($title) { 
     echo $this->title; 
    } 
} 

$recipe1 = new Recipe(); 
    $recipe1->getTitle("chinese tofu noodles"); 
    $recipe1->author; 

$recipe2 = new Recipe(); 
    $recipe2->getTitle("japanese foto maki"); 
    $recipe2->author = "A.B"; 

참고 : 우리가 사유 재산에 액세스하려면 teamtreehous.com에서 비디오에서 AFAIU는 세터 수완 기능이 필요하다.

인쇄되지 않는 이유는 무엇입니까?

답변

3
<?php 

class Recipe { 

    private $title; 
    public $author = "Me myself"; 

    /* Private function for set title */ 
    private function setTitle($title) { 
     echo $this->title = ucwords($title); 
    } 

    /* public function for get title */ 
    public function getTitle($title) { 
     $this->setTitle($title); 
    } 
} 

$recipe = new Recipe(); // creating object 
    $recipe->getTitle("chinese tofu noodles"); // calling function 
    echo "<br>"; 
    echo $recipe->author; 

?> 
0

으로 설정 한 적이 없습니다. 개체 제목입니다. 이 경우에는 아무 것도 출력하지 않는 기능을 사용했습니다. 시나리오에서

조정

<?php 
$recipe1 = new Recipe(); 
//set the title 
$recipe1->setTitle("chinese tofu noodles"); 
//get the title 
$recipe1->getTitle(); 

당신은 get 함수에 대한 인수 할 필요가 없습니다.

0

두 개의 요리법 예제에서는 getTitle을 호출 할 때 제목을 설정하지 않습니다. 또한 getTitle은 함수에서 사용하지 않으므로 인수가 필요하지 않습니다.

저자에게는 단순히 아무 것도 인쇄하지 않습니다.

이 코드는 작동합니다 : 당신은 게터, 세터와 echo을 혼합

class Recipe { 
    private $title; 
    public $author = "Me myself"; 
    public $ingredients = array(); 
    public $instructions = array(); 
    public $tag = array(); 

    public function setTitle($title) { 
     echo $this->title = ucwords($title); 
     echo $this->author; 
    } 
    public function getTitle() { // Removing parameter as it's unused 
     echo $this->title; 
    } 
} 

$recipe1 = new Recipe(); 
    $recipe1->setTitle("chinese tofu noodles"); // set the title 
    $recipe1->getTitle(); // print the title 
    echo $recipe1->author; // Will print "Me myself" 

$recipe2 = new Recipe(); 
    $recipe2->setTitle("japanese foto maki"); // set the title 
    $recipe2->getTitle(); // print the title 
    echo $recipe2->author = "A.B"; // Will print "A.B" 
2

. Getters는 인수를 허용하지 않고 속성을 반환해야합니다. Setters는 인수를 받아들이고 속성을 설정합니다. echo은 (텍스트) 문자열을 화면에 출력합니다.

echo에 대한 문서입니다.

class Recipe { 
    private $title; 
    public $author = "Me myself"; 

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

    public function getTitle() { 
     return $this->title; 
    } 
} 
$noodles = new Recipe(); 
$noodles->setTitle("chinese tofu noodles"); 
echo ($noodles->getTitle); 
//outputs 'chinese tofu noodles' 
관련 문제