例http://ideone.com/c9bY5o
三条件は括弧との検証をラップしている必要がありますプロパティ、存在を区別するために値と検索された値。
(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);
それ以外の場合は、冗長形式を使用すると読みやすくなります。
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
オブジェクトを除去reactions
とshares
特性を有していたことに留意されたいです。
は勧告については、私はあなたがオブジェクトにメソッドを割り当て、合計にその方法を取得代わりに示唆しています。そのため、ある場所で計算を維持し、他のスクリプトでその計算を再利用するだけで済みます。
このようにすれば、データで洗練されたものを他の場所に表示する必要がある場合や、後でオブジェクトや計算に変更や追加を行う場合は、はるかに簡単です。たとえば、投稿に共有がある場合の反応やコメントの合計、または「Up and Down Votes」の追加などです。
ラフレイジーゲッター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'を返します。これは三項演算子に後者の' 0'を返させます。これは元のコードの結果と一致します。私が間違っていない限り? –