PHP - 在另一个对象的数组

时间:2018-05-28 16:50:47

标签: php arrays

我还在学习PHP,所以请原谅我的无知......

我有一个具有3个属性的对象,其中两个是数组。 2个数组中的一个是对象数组。我在设置数组中的属性和对象时遇到了问题。

主对象的var_dump包含以下内容(为了便于识别,我将3个变量分开):

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) } 

我正在尝试在属于fromAddress对象的pendingTransactions数组中设置Transactions属性。

我尝试编写的代码如下:

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;
}

}

非常感谢任何协助。

已阅读以下帖子:

PHP - 获取数组中对象的属性 PHP - get properties of an object in an array? @clerksx @Sampson

访问PHP数组对象保护的属性 Accessing PHP Array Object Protected Property @Aayush @ j0k

Php:似乎无法访问对象中的数组属性 Php: Cannot seem to access array property in object @OllyBarca @Tobias Golbs 编辑:拼写错误的地址 - 之前:fromAddtress

2 个答案:

答案 0 :(得分:2)

我认为你有一些糟糕的设计。 你不能或者我会说收集内部物体是个坏主意,特别是使用coтstructor。

我建议你有两个班级

或在

之外的某个数组中收集项目

或存储不同的类,但存储StdClass或数组作为事务。

解决方案#1:

class Transaction {
  public $fromAddress = "";
  public $toAddress = "";
  public $amount = "";

  public function __construct ($fromAddress, $toAddress, $amount) {
    $this->fromAddress = $fromAddress;
    $this->toAddress = $toAddress;
    $this->amount = $amount;
  }

  public function __toString() {
    return $this->fromAddress . ", " . 
           $this->toAddress . ", " . 
           $this->amount;
  }
}
class TransactionList {
  public $pendingTransactions = [];
  public function __construct ($fromAddress, $toAddress, $amount) {
     $t = new Transaction($fromAddress, $toAddress, $amount);
     $this->pendingTransactions[]=$t;
  }
  public function addTransaction(Transaction $transaction) {
     $this->pendingTransactions[]=$transaction;
  }
}

$tList = new TransactionList('from addr', 'to addr', 'amount');
$tList->addTransaction(new Transaction('from addr 2', 'to addr 2', 'amount2'));
echo ''.$tList->pendingTransactions[1];

解决方案#2:

class Transaction {
  public $fromAddress = "";
  public $toAddress = "";
  public $amount = "";

  public function __construct ($fromAddress, $toAddress, $amount) {
    $this->fromAddress = $fromAddress;
    $this->toAddress = $toAddress;
    $this->amount = $amount;
  }
  public function __toString() {
    return $this->fromAddress . ", " . 
       $this->toAddress . ", " . 
       $this->amount;
  }
}
$tList = [];
$tList[] = new Transaction('from addr', 'to addr', 'amount');
$tList[] = new Transaction('from addr 2', 'to addr 2', 'amount2');
echo ''.$tList[1];

解决方案#3:

class TransactionList {
  public $pendingTransactions = [];
  public function __construct ($fromAddress, $toAddress, $amount) {
     $this->addTransaction($fromAddress, $toAddress, $amount);
  }
  public function addTransaction($fromAddress, $toAddress, $amount) {
     $t = new StdClass();
     $t->fromAddress = $fromAddress;
     $t->toAddress = $toAddress;
     $t->amount = $amount;
     $this->pendingTransactions[]=$t;
  }
}

$tList = new TransactionList('from addr', 'to addr', 'amount');
$tList->addTransaction('from addr 2', 'to addr 2', 'amount2');
echo ''.$tList->pendingTransactions[1]->toAddress;

答案 1 :(得分:0)

您好,您无法访问该属性,因为如果您想访问__construct方法上的对象,您需要先通过参数传递它! 所以你需要这样的东西:

public function __construct($blockChain)
{
}

然后我明白你想要设置对象的一些属性。 首先,您需要了解为了填充对象属性,您必须使用$ this引用它们,在您的代码上使用$ this指向不属于您的类的属性的对象,这就是为什么它不起作用的原因。 例如:

class Transaction {
    public $fromAddress = "";
    public $toAddress = "";
    public $amount = "";
    // here you need to pass the object you want to use when you instantiate the class
    public function __construct ($blockChain)
    { 
       // when using $this you can refer to variables you declared inside your 
       // your class only 
       $this->fromAddress = $blockChain->pendingTransactions[0]->fromAddress
    }
}

我认为你应该查看一些关于如何创建对象的基本理论,因为它可以帮助你入门。 PHP Classes and Objects docs