gpt4 book ai didi

php - 在闭包函数参数PHP中添加类-引发未捕获的TypeError

转载 作者:行者123 更新时间:2023-12-03 08:44:04 25 4
gpt4 key购买 nike

我有3节课

<?php
namespace App;

class Address {

private $houseNumber = '';
private $street = '';
protected $city = '';
protected $county = '';
public $postcode = '';
public $country = '';
private $contacts = [];

public function setHouseNumber($provided_housenumber){
$this->houseNumber = $provided_housenumber;
return $this;
}

public function setStreet($provided_street){
$this->street = $provided_street;
return $this;
}

public function setPostCode($provided_postcode){
$this->postcode = $provided_postcode;
return $this;
}

public function setCounty($provided_county){
$this->county = $provided_county;
return $this;
}

public function setCountry($provided_country){
$this->country = $provided_country;
return $this;
}
}

二等
<?php
namespace App;

class Book {

private $records = [];

public function createAddress(Address $address) {
$this->records[] = $address;
}

}

三等
<?php
namespace App;

class Contact {

private $name = '';
public $email = '';

public function setName($provided_name){
$this->name = $provided_name;
return $this;
}

public function setEmail($provided_mail){
$this->email = $provided_mail;
return $this;
}

}

然后在索引文件中,我这样调用Book方法:
<?php

require_once __DIR__ . '/vendor/autoload.php';

use App\Contact;
use App\Book;
use App\Address;

# Create first contact
$contact = new Contact;
$contact->setName('Mr John Doe');
$contact->setEmail('john@doe.com');

# Add first contact to list of contacts
$contacts[] = $contact;

# Open new book
$book = new Book;

# Add first address with both contacts
$book->createAddress(function(Address $address) use ($contacts){ //<---- Here it throws Fatal error: Uncaught TypeError: Argument 1 passed to App\Book::createAddress() must be an instance of App\Address, instance of Closure given
$address->setHouseNumber('33');
$address->setStreet('Example street')->setCity('Cambridge');
$address->setPostCode('CBF MB5');
$address->setCounty('Cambridgeshire');
$address->setCountry('GB');

foreach($contacts as $contact){
$address->addContact($contact);
}
});

所以我很想在索引文件中做些什么-在Book的新实例中,我正在通过将其全部包装在匿名函数(关闭函数)中来执行方法createAddress()。

在该函数中,我设置了所有变量并将其传递给Book。但是以某种方式我不能在闭包函数中将Class称为参数-

Fatal error: Uncaught TypeError: Argument 1 passed to App\Book::createAddress() must be an instance of App\Address, instance of Closure given



因此,我在这里肯定缺少基本的东西,有人可以指导我完成吗?

最佳答案

public function createAddress(Address $address) // look, it wants an address!

您尝试通过闭包,这是全类同学所不要求的。所以放弃封闭。
$address = new Address();
$address->setHouseNumber('33');
$address->setStreet('Example street')->setCity('Cambridge');
$address->setPostCode('CBF MB5');
$address->setCounty('Cambridgeshire');
$address->setCountry('GB');

foreach ($contacts as $contact){
$address->addContact($contact);
}

$book->createAddress($address);

关于php - 在闭包函数参数PHP中添加类-引发未捕获的TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58818641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com