원래 코드는 금액 예
if ($ExternalPost->reactions->summary->total_count) {
$a = $ExternalPost->reactions->summary->total_count;
} else {
if ($ExternalPost->comments->summary->total_count) {
$a = 0 + $ExternalPost->comments->summary->total_count;
} elseif($ExternalPost->shares->count) {
$a = 0 + $ExternalPost->shares->count
} else {
$a = 0;
}
}
에 http://ideone.com/c9bY5o
삼항 조건의 괄호 및 검증을 포장해야 속성, 존재를 구분하기 위해 ing 값과 검색 값.
(isset($ExternalPost->reactions->summary->total_count) ? $ExternalPost->reactions->summary->total_count : 0) +
(isset($ExternalPost->comments->summary->total_count) ? $ExternalPost->comments->summary->total_count : 0) +
(isset($ExternalPost->shares->count) ? $ExternalPost->shares->count : 0);
그렇지 않으면 읽을 수있는 자세한 정보 형식을 사용하십시오. 개체를 제거하고 reactions
shares
특성을 갖고 있다는
if (isset($ExternalPost->reactions->summary->total_count)) {
$a = $ExternalPost->reactions->summary->total_count;
} else {
$a = 0;
}
if (isset($ExternalPost->comments->summary->total_count)) {
$a += $ExternalPost->comments->summary->total_count;
} else {
$a += 0;
}
if (isset($ExternalPost->shares->count)) {
$a += $ExternalPost->shares->count;
} else {
$a += 0;
}
예 http://ideone.com/GKkpZw
참고.
은 추천에 관해서는, 나는 당신이 개체에 대한 방법을 지정하고 합계 그런 식으로 얻을 대신 좋습니다. 따라서 한 위치에서 계산을 유지하고 다른 스크립트에서 다시 계산할 수 있어야합니다.
그런 식으로 데이터로보다 정교한 작업을하고 싶거나 다른 곳에 표시해야하거나 나중에 개체 나 계산에 변경하거나 추가하기로 결정하면 훨씬 쉬울 것입니다 . 게시물에 공유가있을 때의 반응과 의견을 합산하거나 최대/아래로 투표를 합하는 경우를 예로들 수 있습니다.
거친 레이지 게터 OOP 예 - 등 대신 값의 설정에 합계를 집계 값의 배열을 적용 리팩토링 수
class ExternalPost
{
private $reactions;
private $comments;
private $shares;
public function __construct(Reactions $reactions = null, Comments $comments = null, Shares $shares = null)
{
$this->reactions = $reactions;
$this->comments = $comments;
$this->shares = $shares;
}
public function getReactions()
{
if (null === $this->reactions) {
$this->reactions = new Reactions;
}
return $this->reactions;
}
public function getComments()
{
if (null === $this->comments) {
$this->comments = new Comments;
}
return $this->comments;
}
public function getShares()
{
if (null === $this->shares) {
$this->shares = new Shares;
}
return $this->shares;
}
public function getTotal()
{
return $this->getReactions()->getTotal() +
$this->getComments()->getTotal() +
$this->getShares()->getCount();
}
}
class Summary
{
private $total_count = 0;
public function getTotalCount()
{
return $this->total_count;
}
public function setTotalCount($total)
{
$this->total_count = $total;
return $this;
}
}
abstract class Summation
{
protected $summary;
public function __construct(Summary $summary = null)
{
$this->summary = $summary;
}
public function getSummary()
{
if (null === $this->summary) {
$this->summary = new Summary;
}
return $this->summary;
}
public function getTotal()
{
return $this->getSummary()->getTotalCount();
}
public function setTotal($total)
{
$this->getSummary()->setTotalCount($total);
return $this;
}
}
class Reactions extends Summation{}
class Comments extends Summation{}
class Shares
{
private $count = 0;
public function getCount()
{
return $this->count;
}
public function setCount($count)
{
$this->count = $count;
return $this;
}
}
그래서 이제 스크립트의 코드는 또한 현재의 코드 형식을 유지하기 위해 __set
및 __get
마법 방법을 적용 할 수
$reactions = new Reactions;
$reactions->setTotal(1);
$comments = null;
/*
$comments = new Comments;
$comments->setTotal(2);
//alternative to using getComments below
*/
$ExternalPost = new ExternalPost($reactions, $comments);
/* $ExternalPost->getReactions()->setTotal(1);
//alternative to defining $reactions above */
$ExternalPost->getComments()->getSummary()->setTotalCount(2);
$ExternalPost->getShares()->setCount(3);
$array = [strtotime('today midnight') => $ExternalPost->getTotal()];
예 http://ideone.com/oqSYNd
과 같을 것이다, 그러나 만들면서 그것은 매우 권장하지 않습니다 당신의 코드는 훨씬 덜 쉽게 따라 할 수 있습니다.
'$ ExternalPost-> shares-> count'가'NULL' 인 경우,'isset (...)'는'FALSE'를 반환합니다. 그러면'3' 연산자가 후자 '0'을 반환합니다. 이것은 원래 코드의 결과와 일치합니다. 내가 틀렸다면? –