I still learning PHP, so please forgive my ignorance...
I have an object with 3 properties, two of which are arrays. One of the 2 arrays is an array of objects. I am having an issue setting the properties of and object within the array.
A var_dump of the master object contains the following (I separated the 3 variables for easier recognition):
object(Blockchain)#1 (4) {
["chain"]=> array(1) {
[0]=> object(Block)#2 (5) { ["timestamp"]=> string(10) "05/27/2018" ["transaction"]=> array(2) { ["amount"]=> string(1) "0" ["uid"]=> string(18) "EMG: Genesis Block" } ["previousHash"]=> string(1) "0" ["hash"]=> string(64) "9e2838eb493cc112b2d3fd37925953d25cf9fd2ba4bf7e09750f368451f2ff03" ["nonce"]=> int(0) } }
["difficulty"]=> int(4)
["pendingTransactions"]=> array(2) {
[0]=> object(Transaction)#3 (4) {
["fromAddress"]=> NULL ["toAddress"]=> NULL ["amount"]=> NULL ["pendingTransactions"]=> object(stdClass)#4 (1) { ["tranaction"]=> object(stdClass)#5 (3) { ["fromAddress"]=> string(9) "address-1" ["toAddress"]=> string(9) "address-2" ["amount"]=> int(100) } } }
[1]=> object(Transaction)#6 (4) {
["fromAddress"]=> NULL ["toAddress"]=> NULL ["amount"]=> NULL ["pendingTransactions"]=> object(stdClass)#7 (1) { ["tranaction"]=> object(stdClass)#8 (3) { ["fromAddress"]=> string(9) "address-2" ["toAddress"]=> string(9) "address-1" ["amount"]=> int(25) } } } } ["miningReward"]=> int(100) }
I am trying to set the fromAddress property in the pendingTransactions array that is part of the Transactions object.
The code I attempted to write is as follows:
class Transaction {
public $fromAddress = "";
public $toAddress = "";
public $amount = "";
public function __construct ($fromAddress, $toAddress, $amount) {
$this->pendingTransactions->{Tranaction}->fromAddress = $fromAddress;
$this->pendingTransactions->{Tranaction}->toAddress = $toAddress;
$this->pendingTransactions->{Tranaction}->amount = $amount;
}
public function __toString() {
return $this->pendingTransactions->tranaction->fromAddress . ", " . $this->pendingTransactions->tranaction->toAddress . ", " . $this->pendingTransactions->tranaction->amount;
}
}
Any assistance would be greatly appreciated.
Already read the following posts:
PHP - get properties of an object in an array PHP - get properties of an object in an array? @clerksx @Sampson
Accessing PHP Array Object Protected Property Accessing PHP Array Object Protected Property @Aayush @j0k
Php: Cannot seem to access array property in object Php: Cannot seem to access array property in object @OllyBarca @Tobias Golbs Edit: misspelled fromAddress - before: fromAddtress
$this->pendingTransactionsin__constructno any property is defined yet when you are in constructor. And I don't see anypendingTransactionsproperty defined