2016-07-05 11 views
0

通常のフィールドと入れ子配列を含む次のモデル(オブジェクト)があります。オブジェクトを配列に変換してそのプロパティにアクセスできません

私は、次の

public function getMessageArray() 
{ 
    return (array) $this->message; 
} 

しかし、それは次のような結果に

enter image description hereを与えているとして、いくつかのトリックに遭遇しましたが、完全な配列にモデルを変換しようとしています

もう1つのトリックは、json_encodeとdecodeを次のように使用することです。

$result= json_decode(json_encode($this->message), true); 

しかし、それは空の配列を返しています。

App\Mailer\Models\Message {#505 
    -subject: "You bought products 3 days ago" 
    -fromEmail: "[email protected]" 
    -fromName: "Webshop" 
    -replyTo: "[email protected]" 
    -toEmail: "[email protected]" 
    -toName: "Myself" 
    -mergeVars: array:2 [ 
    "rcpt" => "[email protected]" 
    "vars" => array:2 [ 
     0 => array:2 [ 
     "name" => "fname" 
     "content" => "My" 
     ] 
     1 => array:2 [ 
     "name" => "lname" 
     "content" => "Self" 
     ] 
    ] 
    ] 
} 

これは私がすべてのプロパティにアクセスするために、アレイに$this->messageを変換することはできません$this->message

enter image description here

print_r()です。私は間違って何をしているのだろうか?どんな助けでも大歓迎です。

+0

あなたは 'ますprint_r(の$ this - >メッセージを)'私たちを見ることができますか? – PaulH

+0

追加、thanks Paul – Notflip

+0

オブジェクトのプロパティのすべてがプライベートであるように見えます。メッセージオブジェクトには、データへのアクセスに使用できるメソッドはありませんか? –

答えて

1

属性がプライベートの場合、直接アクセスすることはできません。 json_encode()はそれらをエンコードしません。 下記のPublicMakerクラスは、オブジェクトのすべての属性(プライベートとパブリック)へのアクセスを提供します。

<?php 
/* 
* Make all the attributes of a class public 
*/ 
class PublicMaker { 

    public $matches; 

    function __construct($obj) { 
     // explode object attribute and values 
     $print_r = print_r($obj, true); 
     preg_match_all('/\[(\w*).*\] => (.*)/', $print_r, $matches); 
     $this->matches = $matches; 
    } 

    function getArray() { 
     foreach($this->matches[1] as $key=>$match) { 
      $arr[$match] = $this->matches[2][$key]; 
     } 
     return $arr; 
    } 

    // magic method to get any attribute of the object passed in the contructor 
    function __get($attribute) { 
     $match = array_search($attribute, $this->matches[1]); 
     if ($match !== FALSE) { 
      return $this->matches[2][$match]; 
     } else { 
      return NULL; 
     } 
    } 

} 


// test values 
class Message { 
    private $subject = 'You bought products 3 days ago'; 
    private $fromEmail = "[email protected]"; 
    public $fromName = "Webshop"; 
} 

// usage demonstration 
$message = new Message(); 
$publicMessage = new PublicMaker($message); 
$array = $publicMessage->getArray(); 
$subject = $publicMessage->subject; 

echo '<pre> getArray: '; 
print_r($array); 

echo "\n Subject: $subject \n"; 

// other test values 
echo " 

Other test values:"; 
print_r((array) $message); 
print_r($publicMessage->matches); // debug 
echo "\n subject: ", $publicMessage->subject; // string(30) "You bought products 3 days ago" 
echo "\n fromEmail: ", $publicMessage->fromEmail; 
echo "\n fromName: ", $publicMessage->fromName; 
echo "\n notExist ", $publicMessage->notExist; 
var_dump($publicMessage->notExist); // NULL 

正規表現 がテストされ、ここで絞り込むことができます:http://www.phpliveregex.com/p/gje

関連する問題